Jump to content
  • Advertisement

Archived

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

daher

C++ calling a constuctor

This topic is 5489 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
Advertisement
quote:
Original post by daher
is this valid?


Why don''t you try compiling it and then telling us about it?



AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.

Share this post


Link to post
Share on other sites
I am not asking if it will compile or not, I am asking if this is a good practice...

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!