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

Operator new overloaded

I have overloaded a new operater for a class that returns void * and i have
created an object for that class in heap as below.

CMyClass *pMyClass = new CMyClass(); // Here though i haven't explicitly
casted void* to CMyClass, the compiler doesnt give error at all.

But in c++ for void* to specific type ptr , explicite cast is required. What
was the cause for the above statement to work?

Regards,
Alamelu
Sep 23 '08 #1
1 1242
On Tue, 23 Sep 2008 11:38:10 -0700, Alamelu
<Al*****@discussions.microsoft.comwrote:
>I have overloaded a new operater for a class that returns void * and i have
created an object for that class in heap as below.

CMyClass *pMyClass = new CMyClass(); // Here though i haven't explicitly
casted void* to CMyClass, the compiler doesnt give error at all.

But in c++ for void* to specific type ptr , explicite cast is required. What
was the cause for the above statement to work?
You are confusing the function named "operator new" with the operator named
"new", the latter being what you used in "new CMyClass". The function does
return void*, and the operator calls it to allocate memory. Basically, the
operator does this:

// Allocate memory for an X.
void* p = operator new(sizeof(X));
// Use placement new to construct an X in the memory pointed to by p.
new (p) X;
// Now return a pointer to the dynamically allocated X.
return (X*) p;

That's all legal code, BTW.

P.S. This is not C#, and the parens aren't required when you say "new X()".
Normally, they should be omitted. There is actually a difference between
new X and new X() when (loosely speaking) X is a type with no ctors. The
latter will zero-initialize the object, while the former will leave it
uninitialized.

--
Doug Harrison
Visual C++ MVP
Sep 23 '08 #2

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

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
30
by: | last post by:
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated) before. In general when I have a C++ question I look for answers in "The C++ Programming Language, Third Edition" by Stroustrup....
20
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
2
by: wongjoekmeu | last post by:
Hello All, I have a question about a C++ listing that I don't understand from a book that I use to learn C++. In the listing a class String is declared and defined. The beginning look like this...
7
by: Genival Carvalho | last post by:
Hello friends... Inside my class how do to use explicit using Overloaded operator or use default ? Ex: Dim x as Integer = A + B ' Use default + operator Dim OV as integer = X + Z ' I will...
6
by: idikoko | last post by:
I need help overloafing the <,> operators! I wrote a very simple complex numbers class which basically treats a double as an imaginary number. I wrote the code to overload the >,< operators but...
2
by: B. Williams | last post by:
I have an assignment for school to Overload the operators << and >and I have written the code, but I have a problem with the insertion string function. I can't get it to recognize the second of...
5
by: richard.parker | last post by:
Hello, I need to overload operator new with affecting the system libraries. Has anyone done this? I've got 2 static libraries and application source code where the operator needs to be...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
2
by: subramanian100in | last post by:
overloaded operator=() -------------------------------- overloaded assignment operator should be a non-static MEMBER function of a class. This ensures that the first operand is an lvalue. If...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.