class foo {
foo();
foo(int);
foo(foo& f)
{
foo(f.Int());
}
is this valid? I am a bit confused o_O
____________________________________
MSN | AIM | YIM | ICQ
C++ calling a constuctor
what will calling the constructor do? will it change this class or will it create a new object?
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.
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?
Your code above, from what I can tell, will create a new object, and then immediately destroy it.
In essense, it does nothing.
In essense, it does nothing.
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).
[edited by - Malodorous Skunk on July 5, 2003 4:30:17 PM]
//---------------------------------------------------------------------------#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]
Your code doesn''t make sense at all.
And it''s not valid.
And it won''t compile.
Happy now?
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]
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
Popular Topics
Advertisement