Click here to Skip to main content
15,920,633 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: overloaded operator new and STL vector Pin
Christian Graus22-Oct-02 13:34
protectorChristian Graus22-Oct-02 13:34 
GeneralRe: overloaded operator new and STL vector Pin
Nemanja Trifunovic22-Oct-02 15:58
Nemanja Trifunovic22-Oct-02 15:58 
GeneralRe: overloaded operator new and STL vector Pin
Christian Graus22-Oct-02 16:01
protectorChristian Graus22-Oct-02 16:01 
GeneralRe: overloaded operator new and STL vector Pin
Nemanja Trifunovic22-Oct-02 16:10
Nemanja Trifunovic22-Oct-02 16:10 
GeneralRe: overloaded operator new and STL vector Pin
Christian Graus22-Oct-02 16:17
protectorChristian Graus22-Oct-02 16:17 
GeneralRe: overloaded operator new and STL vector Pin
Nemanja Trifunovic22-Oct-02 18:46
Nemanja Trifunovic22-Oct-02 18:46 
GeneralRe: overloaded operator new and STL vector Pin
Christian Graus22-Oct-02 18:48
protectorChristian Graus22-Oct-02 18:48 
GeneralRe: overloaded operator new and STL vector Pin
Joaquín M López Muñoz22-Oct-02 21:09
Joaquín M López Muñoz22-Oct-02 21:09 
Uff, there's a couple of misunderstandings here.
void* operator new (size_t)
{
return ::new klasa;
}
This is the first problem in your program. An overloaded new operator is meant to be taking care of memory allocation only: the call to the class contructor is generated automatically by the compiler. Please note that the new you provide is calling implicitly the ctor through the use of golbal ::new (altough in this case the error will go unnoticed cause kasa has a trivial ctor). Instead, write something like:
void* operator new (size_t size)
{
return ::new char[size];
}
or
void* operator new (size_t size)
{
return malloc(size);
}
Similarly, your overloaded delete operator should be only concerned with deallocating memory without calling (implicitly) klasa::~klasa.
Now for the second problem. Once you provide an overload for new, the compiler won't use (for the relevant class) any of the predefined global new oeprators available. In particular, it won't use the so-called placement new, whose signature is:
void* operator new (size_t size,void* pos);
Placement new does not really do anything: the caller already provides the space (in pos) where the object is to be constructed. An example of an explicit call to placement new:
void *pos; // pointing to some suitable block of memory
whatever *p=new(pos)whatever;
Now, placement new is extensively used by allocators in STL, and this is what the compiler is complaining about: since you overloaded new you also have to provide an overload for the placement variant:
class klasa{
...
  void* operator new (size_t size,void* pos)
  {
    return pos;
  }
};
Hope I made myself clear enough Smile | :)



Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
GeneralRe: overloaded operator new and STL vector Pin
Nemanja Trifunovic23-Oct-02 5:10
Nemanja Trifunovic23-Oct-02 5:10 
GeneralRe: overloaded operator new and STL vector Pin
Nemanja Trifunovic23-Oct-02 5:36
Nemanja Trifunovic23-Oct-02 5:36 
QuestionHow to enumerate NICs? Pin
Anonymous22-Oct-02 12:11
Anonymous22-Oct-02 12:11 
GeneralDatabase and MFC! HHHEEEELLLLPPPP! Pin
MFC is the Best22-Oct-02 11:58
MFC is the Best22-Oct-02 11:58 
GeneralRe: Database and MFC! HHHEEEELLLLPPPP! Pin
Marc Clifton22-Oct-02 12:17
mvaMarc Clifton22-Oct-02 12:17 
GeneralRe: Database and MFC! HHHEEEELLLLPPPP! Pin
MFC is the Best22-Oct-02 12:43
MFC is the Best22-Oct-02 12:43 
GeneralRe: Database and MFC! HHHEEEELLLLPPPP! Pin
Marc Clifton22-Oct-02 13:13
mvaMarc Clifton22-Oct-02 13:13 
GeneralRe: Database and MFC! HHHEEEELLLLPPPP! Pin
Alexandru Savescu22-Oct-02 20:53
Alexandru Savescu22-Oct-02 20:53 
Questionedit box- change colors? Pin
jimNLX22-Oct-02 11:51
jimNLX22-Oct-02 11:51 
AnswerRe: edit box- change colors? Pin
Joaquín M López Muñoz22-Oct-02 11:54
Joaquín M López Muñoz22-Oct-02 11:54 
AnswerRe: edit box- change colors? Pin
adamUK22-Oct-02 21:45
adamUK22-Oct-02 21:45 
GeneralAssertion Failure Pin
monrobot1322-Oct-02 11:46
monrobot1322-Oct-02 11:46 
GeneralRe: Assertion Failure Pin
Chris Losinger22-Oct-02 11:49
professionalChris Losinger22-Oct-02 11:49 
GeneralRe: Assertion Failure Pin
Joaquín M López Muñoz22-Oct-02 11:50
Joaquín M López Muñoz22-Oct-02 11:50 
GeneralXML & STL Pin
Giles22-Oct-02 11:25
Giles22-Oct-02 11:25 
GeneralRe: XML & STL Pin
Christian Graus22-Oct-02 11:32
protectorChristian Graus22-Oct-02 11:32 
GeneralRe: XML & STL Pin
Giles22-Oct-02 11:41
Giles22-Oct-02 11:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.