By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,296 Members | 1,469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,296 IT Pros & Developers. It's quick & easy.

private constructor and new operator overloading.

P: n/a
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

May 11 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On May 11, 11:27 am, siddhu <siddharth....@gmail.comwrote:
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

May 11 '07 #2

P: n/a
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" <si***********@gmail.com>
??????:11**********************@h2g2000hsg.googleg roups.com...
On May 11, 11:27 am, siddhu <siddharth....@gmail.comwrote:
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


May 11 '07 #3

P: n/a
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" <ji********@msn.com写入消息新闻:f2**********@news. cn99.com...
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" <si***********@gmail.com>
??????:11**********************@h2g2000hsg.googleg roups.com...
>On May 11, 11:27 am, siddhu <siddharth....@gmail.comwrote:
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



May 11 '07 #4

P: n/a
On May 11, 11:27 am, siddhu <siddharth....@gmail.comwrote:
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);
}

May 11 '07 #5

P: n/a
On May 11, 12:03 pm, "Paolo Maldini" <jifang1...@msn.comwrote:
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" <siddharth....@gmail.com>
??????:1178898305.430200.284...@h2g2000hsg.googleg roups.com...
On May 11, 11:27 am, siddhu <siddharth....@gmail.comwrote:
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 -

May 11 '07 #6

P: n/a
On May 11, 12:06 pm, "Paolo Maldini" <jifang1...@msn.comwrote:
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" <jifang1...@msn.com写入消息新闻:f226qn$3s...@news. cn99.com...
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" <siddharth....@gmail.com>
??????:1178898305.430200.284...@h2g2000hsg.googleg roups.com...
On May 11, 11:27 am, siddhu <siddharth....@gmail.comwrote:
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 -

May 11 '07 #7

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

"siddhu" <si***********@gmail.com>
??????:11*********************@n59g2000hsh.googleg roups.com...
On May 11, 12:03 pm, "Paolo Maldini" <jifang1...@msn.comwrote:
>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" <siddharth....@gmail.com>
??????:1178898305.430200.284...@h2g2000hsg.google groups.com...
On May 11, 11:27 am, siddhu <siddharth....@gmail.comwrote:
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 -


May 11 '07 #8

P: n/a
On May 11, 1:43 pm, "Paolo Maldini" <jifang1...@msn.comwrote:
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" <siddharth....@gmail.com>
??????:1178900167.900763.64...@n59g2000hsh.googleg roups.com...
On May 11, 12:03 pm, "Paolo Maldini" <jifang1...@msn.comwrote:
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" <siddharth....@gmail.com>
??????:1178898305.430200.284...@h2g2000hsg.googleg roups.com...
On May 11, 11:27 am, siddhu <siddharth....@gmail.comwrote:
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 -

May 11 '07 #9

P: n/a
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" <ji********@msn.com写入消息新闻:f2**********@news. cn99.com...
After invoking the static member function MyClass::New, the operator new
is called. Then, the constructor is called from the operator new.

"siddhu" <si***********@gmail.com>
??????:11*********************@n59g2000hsh.googleg roups.com...
>On May 11, 12:03 pm, "Paolo Maldini" <jifang1...@msn.comwrote:
>>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" <siddharth....@gmail.com>
??????:1178898305.430200.284...@h2g2000hsg.googl egroups.com...

On May 11, 11:27 am, siddhu <siddharth....@gmail.comwrote:
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 -



May 11 '07 #10

P: n/a
On May 11, 8:05 pm, siddhu <siddharth....@gmail.comwrote:
On May 11, 1:43 pm, "Paolo Maldini" <jifang1...@msn.comwrote:
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.
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.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orient閑 objet/
Beratung in objektorientierter Datenverarbeitung
9 place S閙ard, 78210 St.-Cyr-l'蒫ole, France, +33 (0)1 30 23 00 34

May 11 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.