473,772 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1259
On Tue, 23 Sep 2008 11:38:10 -0700, Alamelu
<Al*****@discus sions.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
17879
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. Why can't the = (assignment) operator be overloaded as a friend function ? I work in VS 6.0 ( Win2000 ) as when i referred the MSDN documen'n it said the following :
30
10450
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. However, I've come upon a question that I can neither answer from "The Book" or a Google search (so yes, at least I RTFBed). I'm hoping that someone in this news group might know the answer. Overloading the Operator Say I want to develop a...
20
4155
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
6403
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 ---------- #include <iostream> #include <string.h> using namespace std; Class String
7
1581
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 need use Overloaded + Operator ??? Thanks and sorry my bad English.
6
1941
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 they don't seem to compile. I'm stuck as to what to do next, please help class Complex { public: Complex( double = 0.0); // constructor Complex operator+( const Complex & ) const; // addition
2
7377
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 number that are input so it selects the values from the default constructor. Can someone assist me with the insertion function. The code is below. // Definition of class Complex #ifndef COMPLEX1_H
5
3482
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 overloaded, but I need to link with the system libraries (frameworks) and I DO NOT want my overloads to be mapped to them. I'm working with some code that is currently working on Win32 but also needs to work on the Mac. Under windows this is very...
5
2293
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 my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
2
2116
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 the overloaded assignment operator function is allowed to be a non- member function then we may be able to write the following: Suppose we have a class Test for which operator+() is defined, suppose we have
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10039
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2851
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.