• 0

Creating a Object from a class automatically.


Question

#include <iostream>
using namespace std;

class test {
  public:
     test(int one, int two);      //concustructor taking two arguments.

  private:
}; one (1, 1)
//one will be created as an ojbect -- but as now the compiler will give me an error

void main()
{

}

Is there a way when this class has been defined that I can create a object with it so the user does not have to?

By that I mean the user will use the class methods, but the object deprived from the class test will have been already created.

So in main I do not have to declare say test one. Sorry I can't be more clearer on this but it is the same way you can do it with structures.

Thanks.

8 answers to this question

Recommended Posts

  • 0

cant test my c++ code, but I think this would work...???

class Test

{

public:

static Test singleton; //static variable to trace instance of class

Test(); //concustructor

Test(int one, int two) //another constructor

public getInstance();

public getInstance(int one, int two); //why would you do this if exists?

public doSomething();

public Test getInstance()

{

if (self.singleton == null)

self.singleton = new Test();

return self.singleton;

}

public Test getInstance(int one, int two)

{

if (self.singleton == null)

self.singleton = new Test(one, two);

return self.singleton;

}

public main

{

Test.getInstance(1, 2).doSomething();

Test.getInstance().doSomething();

}

};

  • 0

Wow -- that looked a lot like Java :), but alas that didn't work. It seems it is on the right track though. I am sure that has to be a way. I basically want to build a class with a pre-defined object from that class that will be used by another class. Sort of like aggregation, but not inheritance. So while others can create different objects from the class -- they won't this time around and will just use the pre-defined object.

Self learning C++ is a lot hard than I thought it would be.

  • 0

Not really a lot of Java at all took 2 minutes...

the key thing is to keep a reference to one instance of your class .. thus the variable

static [insert class name here] singleton;

I'm pretty sure the C++ code I gave above would be close to working, just need to adjust syntax slightly.

another thing people do is construct/init the object when declaring global variable

class X

{

X singleton = new X();

}

Edited by liykh001
  • 0

it's not C++ code. You don't decorate implementation with public. And what is self? It's this. You can't use this with static members. Here's a C++ example of a singleton.

#include <iostream>

class Test
{
public:
	static Test& getInstance();
	void doSomething();
private:
	static Test singleton; // variable to trace instance of class
	Test(){ std::cout << "Inside private constructor" << std::endl; } //concustructor
};

Test Test::singleton;

Test& Test::getInstance()
{
	std::cout << "In getInstance()" << std::endl;
	return singleton;
}

void Test::doSomething()
{
	std::cout << "Did something" << std::endl;
}

int main()
{
	Test t = Test::getInstance();
	t.doSomething();
	return 0;
}

  • 0
it's not C++ code. You don't decorate implementation with public. And what is self? It's this. You can't use this with static members. Here's a C++ example of a singleton.

damm my memory, Thanks

gee it has been a long time since I touched C .... hmm I think it has been about 5 years.

  • 0

int main()
{
 ? ?Test t = Test::getInstance();
 ? ?t.doSomething();
 ? ?return 0;
}

I have a question, in C++

int main()
{
 ? ?//java you can do this
 ? ?Test.getInstance().doSomething();
 ? ?
 ? ?//C++ can you do this?
 ? ?Test::getInstance()::doSomething();

   //or 
   Test::getInstance().doSomething();
 ? ?
 ? ?return 0;
}

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • eh I'll wait for the June 2026 MVS ISO downloads which should be coming out next Tuesday June 16 and possibly contain build 8655 instead of 8653
    • read this recent topic in another forum: https://www.askwoody.com/forums/topic/still-on-win-10-and-happy-to-be-there/ some people are happy sticking with Win10
    • Cooler Master MasterFrame 600 PC case is now 33% off on Amazon by Ivan Jenic The Cooler Master MasterFrame 600 is currently $109.99 on Amazon, down from its original $164.99 list price. That's 33% off and $55 saved on this premium aluminum mid-tower case with a modular design. If you're upgrading your PC case and want something that doesn't force you into a rigid layout, the MasterFrame 600 is worth a look. The case is built around the Cooler Master's FreeForm 2.0 platform, which lets you reconfigure the internal structure according to your hardware. Magnetic side panels allow for straightforward adjustments, and the case supports everything from Mini-ITX to E-ATX motherboards without compromise. There's also generous cooling headroom. Four pre-installed PWM fans handle airflow out of the box. GPU clearance goes up to 410mm, and the case supports radiators up to 420mm with room for three simultaneously. Truth be told, this might not be the prettiest case on the market, but it’s highly functional. The aluminum construction keeps the whole thing lightweight despite its size, and the finish looks noticeably better than the plastic mid-towers competing at this price point. If you want a serious, flexible case that prioritizes function over flashy aesthetics like RGB lighting, the MasterFrame 600 delivers at a reasonable price. Cooler Master MasterFrame 600 - $109.99 | 33% off on Amazon This Amazon deal is US-specific and not available in other regions unless specified. This is a first-party seller link (at the time of article publishing); ensure that you also purchase from a first-party seller link only. If you don't like it or want to look at more options, check out the previous deals that we have covered, OR you can also visit Amazon US deals page. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • DK, I don't use the extended channel, I'm always on the latest release.
  • Recent Achievements

    • Rookie
      restore went up a rank
      Rookie
    • Very Popular
      AndrewSteel earned a badge
      Very Popular
    • Veteran
      Taliseian went up a rank
      Veteran
    • One Month Later
      Clizby earned a badge
      One Month Later
    • One Month Later
      Timaximus earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      509
    2. 2
      +Edouard
      162
    3. 3
      PsYcHoKiLLa
      155
    4. 4
      ATLien_0
      82
    5. 5
      Steven P.
      79
  • Tell a friend

    Love Neowin? Tell a friend!