• 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.

Link to comment
Share on other sites

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();

}

};

Link to comment
Share on other sites

  • 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.

Link to comment
Share on other sites

  • 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
Link to comment
Share on other sites

  • 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;
}

Link to comment
Share on other sites

  • 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.

Link to comment
Share on other sites

  • 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;
}

Link to comment
Share on other sites

  • 0

Yep. that'll work,too, well, at least the Test::getInstance().doSomething(); will. :: is for scope resolution, so if Test resided in a namespace called TestCode, it would look like TestCode::Test::getInstance().doSomething();

Link to comment
Share on other sites

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

    • No registered users viewing this page.