Calling the constructor
-
Hi guys, I have a problem concerning a constructor. I have a code like this:
CClass *pClass;
pClass = new CClass();
pClass->CClass::CClass( param1, param2 );CClass has 2 constructors, the first called with new, and the second one has to be called after it. The VC compiler accepts this syntax, but GCC does not. What can I do? Redesign won't work, I need it exactly like this!! Thanks for advice, Alex Don't try it, just do it! ;-)
-
Hi guys, I have a problem concerning a constructor. I have a code like this:
CClass *pClass;
pClass = new CClass();
pClass->CClass::CClass( param1, param2 );CClass has 2 constructors, the first called with new, and the second one has to be called after it. The VC compiler accepts this syntax, but GCC does not. What can I do? Redesign won't work, I need it exactly like this!! Thanks for advice, Alex Don't try it, just do it! ;-)
Simply speaking, you can't. I am actually surprised that it compiles with VC++.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
Simply speaking, you can't. I am actually surprised that it compiles with VC++.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
I have to and I know that it works for GCC, too... I just don't know the syntax, that's the problem. Don't try it, just do it! ;-)
-
I have to and I know that it works for GCC, too... I just don't know the syntax, that's the problem. Don't try it, just do it! ;-)
You are not really calling 2 constructors, you are only calling one, then calling an function, which happens to be an overloaded constructor. Overloading functions is ok, but some compilers will complain if you try overloading a constructor. Perhaps you should create just the one and pass null values to the first call. I'm curious to know why you need it to be like this?
-
You are not really calling 2 constructors, you are only calling one, then calling an function, which happens to be an overloaded constructor. Overloading functions is ok, but some compilers will complain if you try overloading a constructor. Perhaps you should create just the one and pass null values to the first call. I'm curious to know why you need it to be like this?
CClass *pClass;
pClass = new CClass();
pClass->CClass::CClass( param1, param2 );What is it you are trying to do here? You are creating a pointer to an object, calling its default constructor which should be
pClass = new CClass; // no ()
then trying to call it's overloaded constructor. You can only call one of them, calling the second will only initialise a new object. I'm surprised this compiled.
-
You are not really calling 2 constructors, you are only calling one, then calling an function, which happens to be an overloaded constructor. Overloading functions is ok, but some compilers will complain if you try overloading a constructor. Perhaps you should create just the one and pass null values to the first call. I'm curious to know why you need it to be like this?
there is no way to re-design it.. i need it exactly as i described!!! i solved the problem using inline assembler now, but it is not the best solution i think. The problem is not the overloaded constructor. the problem is, that the GCC does not want me to call a constructor. Don't try it, just do it! ;-)
-
Hi guys, I have a problem concerning a constructor. I have a code like this:
CClass *pClass;
pClass = new CClass();
pClass->CClass::CClass( param1, param2 );CClass has 2 constructors, the first called with new, and the second one has to be called after it. The VC compiler accepts this syntax, but GCC does not. What can I do? Redesign won't work, I need it exactly like this!! Thanks for advice, Alex Don't try it, just do it! ;-)
-
Hi guys, I have a problem concerning a constructor. I have a code like this:
CClass *pClass;
pClass = new CClass();
pClass->CClass::CClass( param1, param2 );CClass has 2 constructors, the first called with new, and the second one has to be called after it. The VC compiler accepts this syntax, but GCC does not. What can I do? Redesign won't work, I need it exactly like this!! Thanks for advice, Alex Don't try it, just do it! ;-)
Why would you want to do such a thing? Steve
-
Hi guys, I have a problem concerning a constructor. I have a code like this:
CClass *pClass;
pClass = new CClass();
pClass->CClass::CClass( param1, param2 );CClass has 2 constructors, the first called with new, and the second one has to be called after it. The VC compiler accepts this syntax, but GCC does not. What can I do? Redesign won't work, I need it exactly like this!! Thanks for advice, Alex Don't try it, just do it! ;-)
You can call any ctor with
new
:pClass = new CClass ( param1, param2 );
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
CClass *pClass;
pClass = new CClass();
pClass->CClass::CClass( param1, param2 );What is it you are trying to do here? You are creating a pointer to an object, calling its default constructor which should be
pClass = new CClass; // no ()
then trying to call it's overloaded constructor. You can only call one of them, calling the second will only initialise a new object. I'm surprised this compiled.
waldermort wrote:
pClass = new CClass();
nop walder, this line is correct. > using no parameter will always implicitely call the default constructor. > using parenthesis without parameters will call the same default constructor, but explicitely. > using some parenthesis with parameters will make the compiler look for the best fitting constructor. you must know that some constructors can be declared as
explicit
, and such constructors cannot be called without parenthesis !!!!