C++ calling a constuctor

Started by
23 comments, last by daher 20 years, 9 months ago
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
____________________________________MSN | AIM | YIM | ICQ
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.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
I am not asking if it will compile or not, I am asking if this is a good practice...
____________________________________MSN | AIM | YIM | ICQ
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?
then I am extreamly confused...

[end of thread]

cya
____________________________________MSN | AIM | YIM | ICQ
Your code above, from what I can tell, will create a new object, and then immediately destroy it.

In essense, it does nothing.
daerid@gmail.com
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 argsusedint main(int argc, char* argv[]){        Foo myFoo;        cin.get();         return 0;}//---------------------------------------------------------------------------



[edited by - Malodorous Skunk on July 5, 2003 4:30:17 PM]
And Dave did see the ad of penis enlargement, and gazing upon His holy manhood, He did thrust the spam into the firey depths of hell.- Book of Dave, 1:7
Your code doesn''t make sense at all.
And it''s not valid.
And it won''t compile.

Happy now?
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]
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.

This topic is closed to new replies.

Advertisement