Connecting Tech Pros Worldwide Forums | Help | Site Map

Calling a constructor?

JKop
Guest
 
Posts: n/a
#1: Jul 22 '05

Anyone know a way of calling a constructor?

At first, I was going to post the following code and ask if it was fully
portable and all, but then it wouldn't compile.


#include <cstdlib>


class AnyClass {};


int main()
{
AnyClass& blah = *static_cast<AnyClass*>
( std::malloc( sizeof(AnyClass) ) );

blah.AnyClass();


//Work with it as normal


blah.~AnyClass();


std::free(&blah);

}


G++ gives the stupid reply:


mem.cpp: In function `int main()':
mem.cpp:14: calling type `AnyClass' like a method


Does anyone know of a compiler that gives meaningful explanations for errors
and warnings? It doesn't necessarily have to produce anything, just
"analyzes" the code and gives the errors and warnings, along with a helpful
explanation.


-JKop

JKop
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Calling a constructor?


JKop posted:
[color=blue]
>
> Anyone know a way of calling a constructor?
>
> At first, I was going to post the following code and ask[/color]
if it was[color=blue]
> fully portable and all, but then it wouldn't compile.
>
>
> #include <cstdlib>
>
>
> class AnyClass {};
>
>
> int main()
> {
> AnyClass& blah = *static_cast<AnyClass*>
> ( std::malloc( sizeof(AnyClass) ) );
>
> blah.AnyClass();
>
>
> //Work with it as normal
>
>
> blah.~AnyClass();
>
>
> std::free(&blah);
>
> }
>
>
> G++ gives the stupid reply:
>
>
> mem.cpp: In function `int main()':
> mem.cpp:14: calling type `AnyClass' like a method
>
>
> Does anyone know of a compiler that gives meaningful[/color]
explanations for[color=blue]
> errors and warnings? It doesn't necessarily have to[/color]
produce anything,[color=blue]
> just "analyzes" the code and gives the errors and[/color]
warnings, along with[color=blue]
> a helpful explanation.
>
>
> -JKop[/color]

If I could get that to somehow work, would it work with
classes that have polymorphic functions?


-JKop
JKop
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Calling a constructor?


JKop posted:
[color=blue]
> JKop posted:
>[color=green]
>>
>> Anyone know a way of calling a constructor?
>>
>> At first, I was going to post the following code and ask[/color][/color]
if it was[color=blue][color=green]
>> fully portable and all, but then it wouldn't compile.
>>
>>
>> #include <cstdlib>
>>
>>
>> class AnyClass {};
>>
>>
>> int main()
>> {
>> AnyClass& blah = *static_cast<AnyClass*>
>> ( std::malloc( sizeof(AnyClass) ) );
>>
>> blah.AnyClass();
>>
>>
>> //Work with it as normal
>>
>>
>> blah.~AnyClass();
>>
>>
>> std::free(&blah);
>>
>> }
>>
>>
>> G++ gives the stupid reply:
>>
>>
>> mem.cpp: In function `int main()':
>> mem.cpp:14: calling type `AnyClass' like a method
>>
>>
>> Does anyone know of a compiler that gives meaningful[/color][/color]
explanations for[color=blue][color=green]
>> errors and warnings? It doesn't necessarily have to[/color][/color]
produce anything,[color=blue][color=green]
>> just "analyzes" the code and gives the errors and[/color][/color]
warnings, along[color=blue][color=green]
>> with a helpful explanation.
>>
>>
>> -JKop[/color]
>
> If I could get that to somehow work, would it work with
> classes that have polymorphic functions?
>
>
> -JKop
>[/color]

CORRECTION

virtual functions


-JKop
Robert Bauck Hamar
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Calling a constructor?


* JKop wrote:[color=blue]
>
> Anyone know a way of calling a constructor?
>
> At first, I was going to post the following code and ask if it was fully
> portable and all, but then it wouldn't compile.
>
>
> #include <cstdlib>
>
>
> class AnyClass {};
>
>
> int main()
> {
> AnyClass& blah = *static_cast<AnyClass*>
> ( std::malloc( sizeof(AnyClass) ) );
>
> blah.AnyClass();[/color]

AnyClass& blah = *new (std::malloc(sizeof(AnyClass))) AnyClass;

--
Robert Bauck Hamar
JKop
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Calling a constructor?


Robert Bauck Hamar posted:
[color=blue]
> AnyClass& blah = *new (std::malloc(sizeof(AnyClass)))[/color]
AnyClass;


Yes, that works (having #include <new> ofcourse, but what I
was looking for was complete control. I want to allocate
the memory when I want to, to call the constructor when I
want to, to call the destructor when I want to, to de-
allocate the memory when I want to.

-JKop
Alf P. Steinbach
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Calling a constructor?


* JKop:[color=blue]
> Robert Bauck Hamar posted:
>[color=green]
> > AnyClass& blah = *new (std::malloc(sizeof(AnyClass)))[/color]
> AnyClass;
>
> Yes, that works (having #include <new> ofcourse, but what I
> was looking for was complete control. I want to allocate
> the memory when I want to, to call the constructor when I
> want to, to call the destructor when I want to, to de-
> allocate the memory when I want to.[/color]

That is a _very bad_ idea. Or, in the language of the FAQ, it's
"evil". Especially when you don't see how to do it.

Can you give some context of the problem where this is apparently
needed?

I'm sure it can solved in much better ways, no matter what it is.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
JKop
Guest
 
Posts: n/a
#7: Jul 22 '05

re: Calling a constructor?


Alf P. Steinbach posted:
[color=blue]
> * JKop:[color=green]
>> Robert Bauck Hamar posted:
>>[color=darkred]
>> > AnyClass& blah = *new (std::malloc(sizeof(AnyClass)))[/color]
>> AnyClass;
>>
>> Yes, that works (having #include <new> ofcourse, but[/color][/color]
what I[color=blue][color=green]
>> was looking for was complete control. I want to allocate
>> the memory when I want to, to call the constructor when[/color][/color]
I[color=blue][color=green]
>> want to, to call the destructor when I want to, to de-[/color][/color]
allocate the[color=blue][color=green]
>> memory when I want to.[/color]
>
> That is a _very bad_ idea. Or, in the language of the[/color]
FAQ, it's[color=blue]
> "evil". Especially when you don't see how to do it.
>
> Can you give some context of the problem where this is[/color]
apparently[color=blue]
> needed?
>
> I'm sure it can solved in much better ways, no matter[/color]
what it is.[color=blue]
>[/color]

No problem per se, just me playing around.

Does it say in the Standard that you can't call a
constructor?


-JKop
Serge Paccalin
Guest
 
Posts: n/a
#8: Jul 22 '05

re: Calling a constructor?


Le dimanche 18 juillet 2004 à 15:11:38, JKop a écrit dans
comp.lang.c++*:
[color=blue]
> Robert Bauck Hamar posted:
>[color=green]
>> AnyClass& blah = *new (std::malloc(sizeof(AnyClass))) AnyClass;[/color]
>
> Yes, that works (having #include <new> ofcourse, but what I
> was looking for was complete control. I want to allocate
> the memory when I want to, to call the constructor when I
> want to, to call the destructor when I want to, to de-
> allocate the memory when I want to.[/color]

You can split the above to do that:

// Allocate only
void *pMem = std::malloc(sizeof (AnyClass));

// Construst only
AnyClass &blah = *new(pMem) AnyClass;

// Destroy only
blah.~AnyClass();

// Release only
std::free(pMem);

But like the others I don't think it's a good idea to go that way.

--
___________ 2004-07-18 15:27:42
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Il faut donc que les hommes commencent
-'(__) par n'être pas fanatiques pour mériter
_/___(_) la tolérance. -- Voltaire, 1763
Robert Bauck Hamar
Guest
 
Posts: n/a
#9: Jul 22 '05

re: Calling a constructor?


* JKop wrote:[color=blue]
> Robert Bauck Hamar posted:
>[color=green]
>> AnyClass& blah = *new (std::malloc(sizeof(AnyClass)))[/color]
> AnyClass;
>
>
> Yes, that works (having #include <new> ofcourse, but what I
> was looking for was complete control.[/color]

Define complete control.
[color=blue]
> I want to allocate
> the memory when I want to,[/color]

You can allocate the memory when you want to.
[color=blue]
> to call the constructor when I
> want to,[/color]

That's what placement new does.
[color=blue]
> to call the destructor when I want to,[/color]

As long as you call it before you deallocate the memory.
[color=blue]
> to de-
> allocate the memory when I want to.[/color]

And where's your problem?
--
Robert Bauck Hamar
Alf P. Steinbach
Guest
 
Posts: n/a
#10: Jul 22 '05

re: Calling a constructor?


* JKop:[color=blue]
> Alf P. Steinbach posted:
>
> No problem per se, just me playing around.
>
> Does it say in the Standard that you can't call a
> constructor?[/color]

That's very irrelevant -- you've been given both technical solutions
and advice regarding what you asked for -- unless you're really
interested in terminology for its own sake?

I only know that it does say you can call a default constructor (see
the FAQ if you don't have the standard, it's actually emphasized there),
and that it uses the word "call" with _more than one_ meaning.

Possibly it also states implicitly somewhere that you cannot e.g.
call a constructor on the 'this' pointer using function call syntax, but
mostly the standard says what you can do, not what you cannot do (the
latter would require an infinite size standard).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Ioannis Vranos
Guest
 
Posts: n/a
#11: Jul 22 '05

re: Calling a constructor?


JKop wrote:[color=blue]
>
> Anyone know a way of calling a constructor?[/color]



There are two books suitable for you: "Accelerated C++" by Andrew
Koenig, Barbara Moo and "The C++ Programming Language" 3rd Edition or
Special Edition by Bjarne Stroustrup.


Keep in mind however that C++ is multiparadigm (it supports 4 paradigms)
and its size is essentially one of four languages (and then some because
most other languages do not fully support their paradigms).



[color=blue]
> At first, I was going to post the following code and ask if it was fully
> portable and all, but then it wouldn't compile.
>
>
> #include <cstdlib>
>
>
> class AnyClass {};
>
>
> int main()
> {
> AnyClass& blah = *static_cast<AnyClass*>
> ( std::malloc( sizeof(AnyClass) ) );[/color]


Don't use malloc() in C++. malloc() does not call the constructors of
the objects. Use new instead.




[color=blue]
>
> blah.AnyClass();
>
>
> //Work with it as normal
>
>
> blah.~AnyClass();
>
>
> std::free(&blah);
>
> }
>
>
> G++ gives the stupid reply:
>
>
> mem.cpp: In function `int main()':
> mem.cpp:14: calling type `AnyClass' like a method[/color]


The constructor is not aimed to be called after the construction
(definition) of an object.


[color=blue]
> Does anyone know of a compiler that gives meaningful explanations for errors
> and warnings? It doesn't necessarily have to produce anything, just
> "analyzes" the code and gives the errors and warnings, along with a helpful
> explanation.[/color]



Ehehe, it should have told you "You can't do that in Horizontal mode!". :-)






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
Closed Thread