Advertisement

Calling constructors

Started by February 24, 2003 01:56 AM
7 comments, last by Peon 21 years, 8 months ago
I''m having a little bit of a problem with calling constructors. I would like to have one class I wrote (containing a different one) to be able to call that objects constructor. For example:
  
class cObject
{
public:
cObject(int x); //constructor

};

cObject::cObject(int x)
{return (x)};

////


class cMan
{
public:
cObject myObject; // << how can I call this constructor?

};  
As you can see, my problem is that while I could call the constructor from within a method, I don''t know how to call it when I declare (which I assume is the only time you can call a constructor?) Thoughts appreciated as always :D Peon
Peon
The constructor is called whenever an instance of a class in created. If I don''t want the objects in the loading part of a game, but in the "init" part, I do:
Class *pC;pC = new Class( param1, param2 ); 

pC is now a pointer to the newly located class.

[s]--------------------------------------------------------[/s]chromecode.com - software with source code
Advertisement
Ok, anytime you create an instance of a class it will call the constructor automatically, as you did in your code. It will only be called once on creation, and therefore you cant publically reference it after that.

Now in your example, the only constructor you used was one taking in an integer variable. In the following class you tried to reference it without one and you tried having it return an integer as well, which i dont think you can do in a constructor.

try this:

  class cObject{public:cObject(int x); //constructor with integercObject();      //base constructor~cObject();     //should always have a deconstructor as wellint GetInt();   //to retrieve your variablevoid SetInt(int x); //to assign a new value after its constructedprivate:int value;      //value of an integer type};cObject::cObject(int x){value = x;}cObject::cObject(){value = 0;}int cObject::GetInt(){return value;}void cObject::SetInt(int x){value = x;}cObject::~cObject(){x = NULL;}// now for your cMan classclass cMan{public:cMan();       // constructor~cMan();      //deconstructor};cMan::CMan(){cObject myObject; // automatically will call the constructor                  // for the NULL constructor since no integer.// or you could do this....cObject myObject(5); // which would assign 5 to value in                      // myObject// or later on you could even do this..myObject.SetInt(5);  // sets value at any given time// if you ever needed to get the value then just callint someInt = myObject.getInt();}  
<=- Talon -=>
Yes, you must create a default constructor. When you declare an object as you have done in the cMan class:

cObject myObject;

i.e. without any parameters, this calls the default constructor. A typical default constructor looks like so:


// ...
public:
cObject(); // default constructor
// ...


You could though keep the constructor that you had:

cObject(int x);

but give it a default value:

cObject(int x = 0);

This is also a default constructor so you can call it both ways:

cObject myObject(46);
cObject myDefaultObject;


For myDefaultObject, x will be initialised to 0 as this is the default value. Good luck.

[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on February 24, 2003 5:22:50 AM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
quote: Original post by Peon
I''m having a little bit of a problem with calling constructors.

You can''t call constructors directly. The compiler does it on your behalf. What you really want to do is to initialise your cObject member. You do it like this:


    class cObject{public:  cObject(int x);};cObject::cObject(int x){  // cannot return a value from constructor!};class cMan{public:  cMan()    : myObject(0)  // initialise myObject  {}  cObject myObject;};    

The above code initialises myObject to the value 0. Please note the two comments I have added, which are both important.
quote: Original post by Lektrix
Yes, you must create a default constructor.

quote: Original post by Californium
[...]

The forum name is "For Beginners" not "By Beginners".

[edited by - SabreMan on February 24, 2003 3:59:56 PM]
Advertisement
Bleh, tits. I really should start reading the whole of the post before answering.

[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
quote: Original post by SabreMan
The forum name is "For Beginners" not "By Beginners".

What can I say but: LOL!


Update GameDev.net system time campaign - success at last
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Thanks a bunch for the above help everyone; exactly what I was looking for. It cleared up / confirmed a lot of my ideas about how constructors worked, as I have not (yet) had any formal teaching in the subject (I can''t wait to go to the university this fall!)

I haven''t tried it yet, but I think what I was looking for was the new operator. I actually tried to use it at first, but tried to do something like:


  cObject myObject;myObject = new cObject(blah);  


...instead of using pointers and such. I''m getting better with pointers I guess, but I still don''t understand them completely, I guess.

Again, thanks a lot for the help!

Peon

Peon

This topic is closed to new replies.

Advertisement