With regard to the following code. I have only included Memory.h in ClassA.cpp, but ClassB.cpp still seems to be able to see it. How do i make it so that ClassB does not use the operator new in Memory.h, but the usual one?
Am am I right in thinking it has something to do with external linkage? But in that case, wouldnt i still have to declare an extern func signature for new in ClassB.cpp?
Basically, Im wanting it so that my overide of operator delete is only used in the source files I include Memory.h in.
//Memory.h
#pragma once
#include <iostream>
void* operator new ( size_t size );
void operator delete ( void* p, int size );
//Memory.cpp
#include "Memory.h"
void* operator new ( size_t size )
{
std::cout << " Allocating " << size << " bytes" << std::endl;
return 0;
}
void operator delete ( void* p, int size )
{
std::cout << " Deallocating " << size << " bytes" << std::endl;
}
//ClassA.h
#pragma once
class ClassA
{
public:
void Hello();
};
//ClassA.cpp
#include "ClassA.h"
#include "Memory.h"
void ClassA::Hello(){
new char();
}
//ClassB.h
#pragma once
class ClassB
{
public:
void Hello();
};
//ClassB.cpp
#include "ClassB.h"
void ClassB::Hello(){
new char();
}