473,396 Members | 1,785 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,396 software developers and data experts.

explicit constructor call on casted void *

Hi, I know there is a proper way to do this but I find myself with a
void pointer returned from a malloc( sizeof( class T ) ) which gives me
a pointer to a memory area of the size of class T. The thing is, I need to
use
this data as a class object, but the constructor hasnt been called which
means I get
a zeroed out vptr table; so I tried this...

void foo(int a) {
T *newTptr = (T *)allocptr(); // returns a void * so casted...
newTptr->T::T( a );
}

The second line explicitly calling the constructor actually works with MS
Visual C++ 7,
but not gcc 3.3.3 .... basically get a parse error.. I was kinda surprised
it worked
and am wondering whether someone can help me get it to work on gcc?

Thanks!
/tq

Jul 22 '05 #1
2 1993
Timo Qvist wrote:
Hi, I know there is a proper way to do this but I find myself with a
void pointer returned from a malloc( sizeof( class T ) ) which gives me
a pointer to a memory area of the size of class T. The thing is, I need to
use
this data as a class object, but the constructor hasnt been called which
means I get
a zeroed out vptr table; so I tried this...

void foo(int a) {
T *newTptr = (T *)allocptr(); // returns a void * so casted...
newTptr->T::T( a );
}

The second line explicitly calling the constructor actually works with MS
Visual C++ 7,
but not gcc 3.3.3 .... basically get a parse error.. I was kinda surprised
it worked
and am wondering whether someone can help me get it to work on gcc?


Use "placement new":

void foo(int a) {
T *newTptr = new (allocptr()) T(a);
}

V
Jul 22 '05 #2
Timo Qvist wrote:
void foo(int a) {
T *newTptr = (T *)allocptr(); // returns a void * so casted...
newTptr->T::T( a );


That's illegal: you cannot call constructors. Use placement new :

void *address = allocptr();
T *p = new (address) T(a);
Jonathan
Jul 22 '05 #3

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

Similar topics

2
by: Ramiraf | last post by:
I have a void * variable1, which is really a pointer to a class. I want to copy the class itself (not just the pointer) onto another void * variable2, using the variable1's class's copy...
2
by: Dario | last post by:
Trying to compile the following code-fragment with g++ 2.96: class Entity { private: void * data; public: explicit Entity(int); explicit Entity(Entity &); virtual ~Entity(); void...
6
by: Christoph Bartoschek | last post by:
Hi, gcc 3.4 rejects the following program: class T { public: T() : a(3) {} explicit T(T const & other) : a(other.a) {} private: int a;
5
by: BCC | last post by:
In looking through some code I have inherited, I notice a lot of places where the programmer used operator() as a function call: void operator() (int x, int y); Rather than an explicit...
12
by: Marcelo Pinto | last post by:
Hi all, In practice, what is the diference between a default constructor and an explicit default constructor? class Ai { public: Ai() {} };
4
by: away | last post by:
1. When a class defined with a member of pointer type, it's necessary to have a copy constructor and assignment operator. If don't pass objects of such class as value in a function and don't do...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
1
by: denis | last post by:
Hello, I noticed the following in the "C++ Coding Standards" book. Why should the code on line 30 be in error? Transmogrify accepts a (B obj), and we are passing it a B obj. I understand why...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.