473,385 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Calling a constructor?


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
Jul 22 '05 #1
10 1378
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
Jul 22 '05 #2
JKop posted:
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


CORRECTION

virtual functions
-JKop
Jul 22 '05 #3
* JKop wrote:

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;

--
Robert Bauck Hamar
Jul 22 '05 #4
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
Jul 22 '05 #5
* 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.


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?
Jul 22 '05 #6
Alf P. Steinbach posted:
* 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.


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
Jul 22 '05 #7
Le dimanche 18 juillet 2004 à 15:11:38, JKop a écrit dans
comp.lang.c++*:
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.


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
Jul 22 '05 #8
* JKop wrote:
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.


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?
--
Robert Bauck Hamar
Jul 22 '05 #9
* 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).

--
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?
Jul 22 '05 #10
JKop wrote:

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
Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Murat Tasan | last post by:
i have a quick question... is there a way to obtain the reference to the object which called the currently executing method? here is the scenario, i have a class and a field which i would like to...
9
by: Giulio | last post by:
why definition of two constructors like these is not possible in c++??? ----------------------- date::date(const int d, const int m, const int y, const int ora, const int mi, const int se){...
2
by: William Payne | last post by:
Hello, consider these following two classes. A base class, class MDIChildWindow, and a class inherting from that base class, class Document. In the static base member function callback() I obtain a...
4
by: Jerry Krinock | last post by:
I've written the following demo to help me understand a problem I'm having in a larger program. The "main" function constructs a Foo object, and then later "reconstructs" it by calling the...
6
by: Justin | last post by:
Hello, first time posting. If I have a base class and a derived class, is there only one way to call the base constructor? i.e. Is this the only way I can call the base constructor...
8
by: Greg Bacchus | last post by:
I have a base class with a method that is to be called in the constructor of the inheritting classes. Is there any way of determining, say, the Type of the class that is calling it. e.g. ...
12
by: st_ev_fe | last post by:
I've noticed that when constructing a subclass, the base class get's it's contructors called. Is there some way to avoid this? The base class has one variable, which gets initialised to 0. ...
6
by: daveb | last post by:
I'm trying to write some code that calls the constructors of STL containers explicitly, and I can't get it to compile. A sample program is below. One compiler complains about the last two lines...
1
by: newbie | last post by:
This is a snippet from C++ FAQs, which I have never done--- when I do such a thing, I would declare function used by constructor ( in this example, init() ) as static. But I do understand that it...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.