Calling a constructor?

J

JKop

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
 
J

JKop

JKop posted:
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

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


-JKop
 
J

JKop

JKop posted:
JKop posted:


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


-JKop

CORRECTION

virtual functions


-JKop
 
R

Robert Bauck Hamar

* JKop said:
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();

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

JKop

Robert Bauck Hamar posted:
AnyClass& blah = *new (std::malloc(sizeof(AnyClass)))
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
 
A

Alf P. Steinbach

* JKop:
Robert Bauck Hamar posted:

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.

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.
 
J

JKop

Alf P. Steinbach posted:
* JKop:

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.

No problem per se, just me playing around.

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


-JKop
 
S

Serge Paccalin

Le dimanche 18 juillet 2004 à 15:11:38, JKop a écrit dans
comp.lang.c++ :
Robert Bauck Hamar posted:


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.

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
 
R

Robert Bauck Hamar

* JKop said:
Robert Bauck Hamar posted:

AnyClass;


Yes, that works (having #include <new> ofcourse, but what I
was looking for was complete control.

Define complete control.
I want to allocate
the memory when I want to,

You can allocate the memory when you want to.
to call the constructor when I
want to,

That's what placement new does.
to call the destructor when I want to,

As long as you call it before you deallocate the memory.
to de-
allocate the memory when I want to.

And where's your problem?
 
A

Alf P. Steinbach

* JKop:
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?

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).
 
I

Ioannis Vranos

JKop said:
Anyone know a way of calling a constructor?



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).



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) ) );


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




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


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


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.



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






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top