private constructor and new operator overloading.

S

siddhu

Dear Experts,

I want to make a class whose objects can be created only on heap.
I used the following approach.

class ss
{
ss(){}
public:
void* operator new(size_t sz)
{
cout<<"in new"<<endl;
return new char[sz];
}
void operator delete(void* m)
{
free(m);
}

};
int main()
{

ss* s = new ss;
return 0;
}

But when I do
ss* s = new ss;
compiler complains that constructor ss::ss() is private.
As operator new function is member of class so compiler should not
complain because operator new function can call the default
constructor even if it is private.
I am surprised and confused.
Suggestions would be of great help.

Regards,
Siddharth
 
S

siddhu

Dear Experts,

I want to make a class whose objects can be created only on heap.
I used the following approach.

class ss
{
ss(){}
public:
void* operator new(size_t sz)
{
cout<<"in new"<<endl;
return malloc(sz); //return new char[sz];
//sorry..... my
mistake
 
P

Paolo Maldini

you need to write a new static member function to instead of the operator
new.

for instance:
class MyClass {
public:
static MyClass* New();
// using keyword "protected" means it can be derived.
// using keyword "private" means it cannot be derived any more.
protected:
MyClass();
};

int main() {
....
MyClass* pMyClass = MyClass::New();
....
delete pMyClass;
....
}

siddhu said:
Dear Experts,

I want to make a class whose objects can be created only on heap.
I used the following approach.

class ss
{
ss(){}
public:
void* operator new(size_t sz)
{
cout<<"in new"<<endl;
return malloc(sz); //return new char[sz];
//sorry..... my
mistake
}
void operator delete(void* m)
{
free(m);
}

};

int main()
{

ss* s = new ss;
return 0;

}

But when I do
ss* s = new ss;
compiler complains that constructor ss::ss() is private.
As operator new function is member of class so compiler should not
complain because operator new function can call the default
constructor even if it is private.
I am surprised and confused.
Suggestions would be of great help.

Regards,
Siddharth
 
P

Paolo Maldini

further to last mail.

to implement the static member function, you may write the code like this:
MyClass* MyClass::New() {
return new MyClass();
}

Paolo Maldini said:
you need to write a new static member function to instead of the operator
new.

for instance:
class MyClass {
public:
static MyClass* New();
// using keyword "protected" means it can be derived.
// using keyword "private" means it cannot be derived any more.
protected:
MyClass();
};

int main() {
...
MyClass* pMyClass = MyClass::New();
...
delete pMyClass;
...
}

siddhu said:
Dear Experts,

I want to make a class whose objects can be created only on heap.
I used the following approach.

class ss
{
ss(){}
public:
void* operator new(size_t sz)
{
cout<<"in new"<<endl;
return malloc(sz); //return new char[sz];
//sorry..... my
mistake
}
void operator delete(void* m)
{
free(m);
}

};

int main()
{

ss* s = new ss;
return 0;

}

But when I do
ss* s = new ss;
compiler complains that constructor ss::ss() is private.
As operator new function is member of class so compiler should not
complain because operator new function can call the default
constructor even if it is private.
I am surprised and confused.
Suggestions would be of great help.

Regards,
Siddharth
 
S

Salt_Peter

Dear Experts,

I want to make a class whose objects can be created only on heap.
I used the following approach.

class ss
{
ss(){}
public:
void* operator new(size_t sz)
{
cout<<"in new"<<endl;
return new char[sz];
}
void operator delete(void* m)
{
free(m);
}

};

int main()
{

ss* s = new ss;
return 0;

}

But when I do
ss* s = new ss;
compiler complains that constructor ss::ss() is private.
As operator new function is member of class so compiler should not
complain because operator new function can call the default
constructor even if it is private.
I am surprised and confused.
Suggestions would be of great help.

Regards,
Siddharth

These dynamic containers also store their elements on the heap.

#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <list>

class A { };

int main()
{
std::vector< int > vn(100, 0); // 100 integers initialized to 0
std::deque< std::string > ds(10, "default string"); // 10 strings
std::list< A > la(20);
}
 
S

siddhu

you need to write a new static member function to instead of the operator
new.

But How the constructor gets called?Is it called from operator new
function or is it called after operator new function?

for instance:
class MyClass {
public:
static MyClass* New();
// using keyword "protected" means it can be derived.
// using keyword "private" means it cannot be derived any more.
protected:
MyClass();

};

int main() {
...
MyClass* pMyClass = MyClass::New();
...
delete pMyClass;
...

}

"siddhu" <[email protected]>
??????:[email protected]...


Dear Experts,
I want to make a class whose objects can be created only on heap.
I used the following approach.
class ss
{
ss(){}
public:
void* operator new(size_t sz)
{
cout<<"in new"<<endl;
return malloc(sz); //return new char[sz];
//sorry..... my
mistake
}
void operator delete(void* m)
{
free(m);
}
};
int main()
{
ss* s = new ss;
return 0;
}
But when I do
ss* s = new ss;
compiler complains that constructor ss::ss() is private.
As operator new function is member of class so compiler should not
complain because operator new function can call the default
constructor even if it is private.
I am surprised and confused.
Suggestions would be of great help.
Regards,
Siddharth- Hide quoted text -

- Show quoted text -
 
S

siddhu

further to last mail.

to implement the static member function, you may write the code like this:
MyClass* MyClass::New() {
return new MyClass();

Does it mean that constructor is getting called from this static
member function?
}

"Paolo Maldini" <[email protected]> дÈëÏûÏ¢ÐÂÎÅ:[email protected]...


you need to write a new static member function to instead of the operator
new.
for instance:
class MyClass {
public:
static MyClass* New();
// using keyword "protected" means it can be derived.
// using keyword "private" means it cannot be derived any more.
protected:
MyClass();
};
int main() {
...
MyClass* pMyClass = MyClass::New();
...
delete pMyClass;
...
}
siddhu said:
A correction in the code snippet..
Dear Experts,
I want to make a class whose objects can be created only on heap.
I used the following approach.
class ss
{
ss(){}
public:
void* operator new(size_t sz)
{
cout<<"in new"<<endl;
return malloc(sz); //return new char[sz];
//sorry..... my
mistake
}
void operator delete(void* m)
{
free(m);
}
};
int main()
{
ss* s = new ss;
return 0;
}
But when I do
ss* s = new ss;
compiler complains that constructor ss::ss() is private.
As operator new function is member of class so compiler should not
complain because operator new function can call the default
constructor even if it is private.
I am surprised and confused.
Suggestions would be of great help.
Regards,
Siddharth- Hide quoted text -

- Show quoted text -
 
P

Paolo Maldini

After invoking the static member function MyClass::New, the operator new is
called. Then, the constructor is called from the operator new.

siddhu said:
you need to write a new static member function to instead of the operator
new.

But How the constructor gets called?Is it called from operator new
function or is it called after operator new function?

for instance:
class MyClass {
public:
static MyClass* New();
// using keyword "protected" means it can be derived.
// using keyword "private" means it cannot be derived any more.
protected:
MyClass();

};

int main() {
...
MyClass* pMyClass = MyClass::New();
...
delete pMyClass;
...

}

"siddhu" <[email protected]>
??????:[email protected]...


A correction in the code snippet..
Dear Experts,
I want to make a class whose objects can be created only on heap.
I used the following approach.
class ss
{
ss(){}
public:
void* operator new(size_t sz)
{
cout<<"in new"<<endl;
return malloc(sz); //return new char[sz];
//sorry..... my
mistake
}
void operator delete(void* m)
{
free(m);
}

int main()
{
ss* s = new ss;
return 0;

But when I do
ss* s = new ss;
compiler complains that constructor ss::ss() is private.
As operator new function is member of class so compiler should not
complain because operator new function can call the default
constructor even if it is private.
I am surprised and confused.
Suggestions would be of great help.
Regards,
Siddharth- Hide quoted text -

- Show quoted text -
 
S

siddhu

After invoking the static member function MyClass::New, the operator new is
called. Then, the constructor is called from the operator new.

If it is called from operator new and if operator new is class member
function then why can't it call private default constructor.
And also in your case how operator ::new can call private/protected
default constructor?
"siddhu" <[email protected]>
??????:[email protected]...


But How the constructor gets called?Is it called from operator new
function or is it called after operator new function?
for instance:
class MyClass {
public:
static MyClass* New();
// using keyword "protected" means it can be derived.
// using keyword "private" means it cannot be derived any more.
protected:
MyClass();
};
int main() {
...
MyClass* pMyClass = MyClass::New();
...
delete pMyClass;
...
}
"siddhu" <[email protected]>
??????:[email protected]...
A correction in the code snippet..
Dear Experts,
I want to make a class whose objects can be created only on heap.
I used the following approach.
class ss
{
ss(){}
public:
void* operator new(size_t sz)
{
cout<<"in new"<<endl;
return malloc(sz); //return new char[sz];
//sorry..... my
mistake
}
void operator delete(void* m)
{
free(m);
}
};
int main()
{
ss* s = new ss;
return 0;
}
But when I do
ss* s = new ss;
compiler complains that constructor ss::ss() is private.
As operator new function is member of class so compiler should not
complain because operator new function can call the default
constructor even if it is private.
I am surprised and confused.
Suggestions would be of great help.
Regards,
Siddharth- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
P

Paolo Maldini

i think maybe i made a mistake.
the constructor is called after the operator new.

when we are writing the code:
MyClass* MyClass::New()
{
MyClass* pMyClass = new MyClass();
return pMyClass;
}

the compiler maybe explained like this:
MyClass* MyClass::New()
{
MyClass* pMyClass = (MyClass*)malloc(sizeof(MyClass));
pMyClass->MyClass();
return pMyClass;
}

so i think actually the constructor is called by MyClass:New();

Paolo Maldini said:
After invoking the static member function MyClass::New, the operator new
is called. Then, the constructor is called from the operator new.

siddhu said:
you need to write a new static member function to instead of the
operator
new.

But How the constructor gets called?Is it called from operator new
function or is it called after operator new function?

for instance:
class MyClass {
public:
static MyClass* New();
// using keyword "protected" means it can be derived.
// using keyword "private" means it cannot be derived any more.
protected:
MyClass();

};

int main() {
...
MyClass* pMyClass = MyClass::New();
...
delete pMyClass;
...

}

"siddhu" <[email protected]>
??????:[email protected]...



A correction in the code snippet..
Dear Experts,

I want to make a class whose objects can be created only on heap.
I used the following approach.

class ss
{
ss(){}
public:
void* operator new(size_t sz)
{
cout<<"in new"<<endl;
return malloc(sz); //return new char[sz];
//sorry..... my
mistake
}
void operator delete(void* m)
{
free(m);
}

};

int main()
{

ss* s = new ss;
return 0;

}

But when I do
ss* s = new ss;
compiler complains that constructor ss::ss() is private.
As operator new function is member of class so compiler should not
complain because operator new function can call the default
constructor even if it is private.
I am surprised and confused.
Suggestions would be of great help.

Regards,
Siddharth- Hide quoted text -

- Show quoted text -
 
J

James Kanze

If it is called from operator new and if operator new is class member
function then why can't it call private default constructor.

This is a common misconception. There are two distinct things:
new expressions, and the operator new() function. A new
expression does two things: it looks up an appropriate
"allocator" (a function whose name is spelled "operator new"),
calls that, and then calls the appropriate constructor. Access
is verified for both, at the point of the new expression.

If you want to ensure allocation on the heap, the classical
solution is to declare the destructor private, and to provide a
member function which simply calls delete this. This also
prevents derivation, of course.

The terminology is in fact quite confusing, since the operator
new() function does not really act like the operator "new" in
the language (which is really very special as operators go
anyway). At one point in the standard, it was decided to speak
about allocator functions, instead of operator new() functions,
in order to make the distinction clearer. But since for
historical reasons, the name of an allocator function is
"operator new", even this doesn't help as much as one would
like.
And also in your case how operator ::new can call private/protected
default constructor?

See above. Access to the selected operator new and the
constructor are both verified at the point of the new
expression. The operator new function is an allocator, which
has nothing to do with the constructor.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top