Archived

This topic is now archived and is closed to further replies.

C++ calling a constuctor

This topic is 5278 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

what will calling the constructor do? will it change this class or will it create a new object?
class foo {
  foo();
  foo(int);
  foo(foo& f)
  {
     foo(f.Int());
  }
is this valid? I am a bit confused o_O
____________________________________ Quake3 Is teh best MSN | AIM | YIM | ICQ

Share this post


Link to post
Share on other sites
quote:
Original post by daher
what will calling the constructor do? will it change this class or will it create a new object?
...
{source}
...
is this valid? I am a bit confused o_O


You never asked if it was good practice, only questions that could be answered by compiling ("is this valid?") and if it compiled, running a few tests to see what actually happened.

You''re still confused as to what it does, how can you be asking if it''s good practice?

Share this post


Link to post
Share on other sites
Your code above, from what I can tell, will create a new object, and then immediately destroy it.

In essense, it does nothing.

Share this post


Link to post
Share on other sites
A constructor is called when an instance of a class or struct is declared. A destructor is called when an object is deleted or terminated (I think).

//---------------------------------------------------------------------------


#pragma hdrstop
#include <iostream>
using namespace std;
//---------------------------------------------------------------------------


struct Foo
{

Foo(){
cout<<"constructing foo"<<endl;
};
~Foo(){
cout<<"Deconstructing foo"<<endl;
};

};
#pragma argsused
int main(int argc, char* argv[])
{


Foo myFoo;

cin.get();
return 0;
}
//---------------------------------------------------------------------------




[edited by - Malodorous Skunk on July 5, 2003 4:30:17 PM]

Share this post


Link to post
Share on other sites
First off. Why in the hell would that ever be needed. And it looks extremely messy and invalid. Therefore, no, its not good practice at all. It kinda looks like a copy constructor, but youre creating yet another instance of that class. Why don't you try this.

class foo{
foo(foo& f){ foo(*f); }
...

Now thats interesting stuff right there.


[edited by - TheJakub on July 5, 2003 4:39:26 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by TheJakub
Why don''t you try this.

class foo{
foo(foo& f){ foo(*f); }
...

Now thats interesting stuff right there.


That won''t compile unless you''ve defined operator*() for foo.
</nit-pick>

@daher: It''s valid, but pointless. It will create create a temporary object of type foo, calling the constructor foo(int). Assuming that Int() returns an int, that is.

There are situations where calling a constructor explicitly can be useful. For example, if you have a function:

int foo(CFoo& f)
{
...
}

and foo has a constructor foo(int, float), you can call
foo(CFoo(5, 2.0f));

Which will create an object with those constructor arguments, and pass it to the function.

Share this post


Link to post
Share on other sites
Hi

Well here are some tips, hope they come of some use.

At first a class constructor is automaticly created by your compiler(well it should). So if you don't create your own constructor for a class the compiler will create one for you. This allso applies to deconstructor. Once again if you don't define one by yourself the compiler should do one for you. By the way different compiler are more strick when it comes to languages syntax. For example some versions of Visual Studio may or may have not allowed some things to be done which are not possible on others. Well this may not actually apply to languages syntax, but be carefull. One example of this is VS lets you define an variable as 'bool'. If I remember correctly there is no such variable type in C and C++. Well I tried it with g++ on linux and it didn't work. Anyway be carefull.

Ok back to constructors. Now lets say you want to define your own construct. First rule is that it should be the same name as the classes name. In VS if you create your own constructor for your class you don't have to define an deconstructor, but I highly recommend it. It is good programming in my opinion. Now as you can create overload function you can allso create overloaded constructors. Notice thought your can have only one decostructor and that must be without any addittional parameters. Look below:



class One
{
public:

One();
One(int);
One(int, char);
~One();

};



The next thing you should take in consideration is the constructor and deconstructors body. Once again in VS you can create the body inside the class definitions. NOTICE: If you give the body to one of the constructors you should do it to all of them and allso to the deconstructor. If you don't do it the depending on your compiler you will receive parse errors. Allso you can not define one constructors body inside the class definition and the others outside. Well if the compiler lets you then don't know, but I don not recommend this. Not only it is confusing(specially later when you may extend your class) it should not be allowed by the compiler.

   

class One
{
public:

One() {} ;
One(int) {};
One(int, char) {};
~One() {};

};

or

class One
{
public:

One() ;
One(int) ;
One(int, char) ;
~One();

};

One::One()
{
}

One::One(int)
{
}

One::One(int, char)
{
}

One::~One()
{

}



The last thing that I have to say is copy constructor. They do what the word copy stands for. By default the compiler creates you an copy constructor, which doesn't do much(well it is called when you make a copy of your class). You can use an copy constructor to copy data into your class or into another class or what ever you define it to do. Here are some situations. When you give ro return an class(object) to a function. In this case the compiler makes an temporarry copy of the class(object). If the class(object) is apart of an self made class then during the copy process the copy constructor is called. Now some rules when creating your own copy constructor. A copy constructor can have only one parameter this must be the same type as the class itself. The parameter should be passed to the copy constructor as an reference and you should define is as a constant value.



class One
{
public:

One();
One(int);
One(int, char);
One(const One &anotherOneClass);
~One();

};

[/souce]

You should be aware of the dangers that come with copy constructors, specially when it comes to copy constructors defined by you. This problem is memory addressing. One of this problem will come forth with classes variables. When you use a copy constructor to copy data and you have variables with pointers on them you will have the problem of memory addressing. If one of the classes will stop exist you will have a pointer from one of the two classes pointing to nowhere and this will cause a crash in the program or memory leaks. Who knows so be carefull.

Well thats all hope you find some info in the text i wrote. If anyone find errors in my text please correct me, nobody is perfect.

Hafe fun.


[edited by - LionheartAdi on July 6, 2003 9:46:29 AM]

[edited by - LionheartAdi on July 6, 2003 9:47:01 AM]

[edited by - LionheartAdi on July 6, 2003 9:47:45 AM]

Share this post


Link to post
Share on other sites
just got an idea
class tfoo {
tfoo() {...};
tfoo(int): tfoo() {...};
tfoo(char, int i): tfoo(i) {...};
};
now that sounds like working...
what do you think?

Share this post


Link to post
Share on other sites

class whatever
{
public:
whatever();
whatever( int );
whatever( const std::string & );
whatever( const whatever & );
...

private:
void init( /* some arguments */ );

};

Just call init() from the constructors..

Share this post


Link to post
Share on other sites
quote:
Original post by cedricl
quote:
Original post by daher
now that sounds like working...
what do you think?

Yes it should.



No sorry, it will still create a temporary object and destory it - check it out by putting logging messages in the destructor...

There is NO way to call another constructor from within another one in C++ - thats FINAL!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by jamessharpe
quote:
Original post by cedricl
quote:
Original post by daher
now that sounds like working...
what do you think?

Yes it should.



No sorry, it will still create a temporary object and destory it - check it out by putting logging messages in the destructor...

There is NO way to call another constructor from within another one in C++ - thats FINAL!


I know this does not apply to the examples used earlier, but using Inheritence\Multiple Inheritence, you can call the Base Class constructor whithin the Derived Class Constructor, so is it just me or does that throw out your There is NO way to call another constructor from whithin another one in C++?

Share this post


Link to post
Share on other sites
And since when is bool not part of C++?? I''m not willing to bet my life on it, but I''m __fairly__ sure the data type ''bool'' is an integral part of C++.

Share this post


Link to post
Share on other sites
Okay, calling the base constructor in inheritance is the exception, but the OP is not using any inheritance. Worded differently - for each object constructed, only one constructor from each class that makes up the object(via inheritance if used) can be called.

Share this post


Link to post
Share on other sites
Best damn description of the matter I've seen so far. Why didn't you say that the first time I asked?

[edited by - Red Ant on July 7, 2003 10:58:02 AM]

Share this post


Link to post
Share on other sites
Read the thread through again, and read this thread:
Clicky

If you still don't understand why, then ask again, but I think the question has been answered.

[edited by - jamessharpe on July 7, 2003 11:39:43 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
I know this does not apply to the examples used earlier, but using Inheritence\Multiple Inheritence, you can call the Base Class constructor whithin the Derived Class Constructor, so is it just me or does that throw out your There is NO way to call another constructor from whithin another one in C++?

No. In that case, you are not calling a ctor, you are specifying a base class initialisation.

Share this post


Link to post
Share on other sites
Yeah a constructor will be called always in inheritance, even if it is a compiler made default one. C++ just gives you the option of controlling which constructor gets called - and the arguments it''s called with.

Share this post


Link to post
Share on other sites