私有构造函数和新运算符重载。 [英] private constructor and new operator overloading.
问题描述
亲爱的专家,
我想创建一个只能在堆上创建对象的类。
我使用了以下方法。
class ss
{
ss(){}
public:
void * operator new(size_t sz)
{
cout<<" in new"<< endl;
return新字符[sz];
}
无效操作符删除(void * m)
{
免费( m);
}
};
int main()
{
ss * s =新ss;
返回0;
}
但是当我这样做
ss * s =新ss;
编译器抱怨构造函数ss :: ss()是私有的。
作为运算符的新函数是类的成员所以编译器不应该抱怨
抱怨,因为operator new函数可以调用默认的
构造函数,即使它是私有的。
我是惊讶和困惑。
建议会有很大的帮助。
问候,
Siddharth
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
推荐答案
5月11日,11:早上27点,siddhu< siddharth .... @ gmail.comwrote:
代码片段中的更正..
On May 11, 11:27 am, siddhu <siddharth....@gmail.comwrote:
A correction in the code snippet..
尊敬的专家,
我想创建一个只能在堆上创建对象的类。
我使用了以下方法。
class ss
{
ss(){}
public:
void * operator new (size_t sz)
{
cout<<" in new"<<< endl;
return malloc(sz); //返回新的字符[sz];
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 =新ss;
返回0;
}
但是当我这样做时
ss * s = new ss;
编译器抱怨构造函数ss :: ss()是私有的。
因为运算符新函数是类的成员所以编译器不应该抱怨
因为操作员新功能可以调用默认的
构造函数,即使它是私有的。
我很惊讶和困惑。 />
建议会有很大的帮助。
问候,
Siddharth
}
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
你需要编写一个新的静态成员函数来代替o perator
new。
例如:
class MyClass {
public:
static MyClass * New();
//使用关键字" protected"表示它可以派生。
//使用关键字private意味着它不能再导出了。
受保护:
MyClass();
};
int main(){
....
MyClass * pMyClass = MyClass :: New();
....
删除pMyClass;
....
}
" siddhu" < si *********** @ gmail.com>
??????:11 *************** *******@h2g2000hsg.googleg roups.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...
5月11日上午11:27,siddhu< siddharth .... @ gmail.comwrote:
代码片段中的更正..
On May 11, 11:27 am, siddhu <siddharth....@gmail.comwrote:
A correction in the code snippet..
>亲爱的专家,
我想创建一个只能在堆上创建对象的类。
我使用了以下方法。
class ss
{
ss(){} <公开:
void * operator new(size_t sz)
{
cout<<<" in new"<<< endl;
return malloc(sz) ; //返回新的字符[sz];
>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)
{
免费(m);
}
};
int main()
{
ss * s =新ss;
返回0;
}
但是什么时候我做了ss * s = new ss;
编译器抱怨构造函数ss :: ss()是私有的。
因为运算符新函数是类的成员所以编译器不应该
抱怨因为操作员新功能可以调用默认的
构造函数,即使它是私有的。
我很惊讶和困惑。
建议会有很大的帮助。
问候,
Siddharth
> }
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
进一步邮寄。
实现静态成员函数,你可以编写如下代码:
MyClass * MyClass :: New(){
返回新的MyClass();
}
" Paolo Maldini" <ジ******** @msn.comD'è????¢d ???:F2 ********** @消息。 cn99.com ...
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.comD′è????¢D???:f2**********@news. cn99.com...
你需要写一个新的静态成员函数而不是运算符
new。
例如:
class MyClass {
public:
static MyClass * New();
//使用关键字protected表示它可以派生。
//使用关键字private意味着它不能再导出了。
受保护:
MyClass();
};
int main(){
...
MyClass * pMyClass = MyClass :: New();
...
删除pMyClass;
...
}
" siddhu" < si *********** @ gmail.com>
??????:11 *************** *******@h2g2000hsg.googleg roups.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...
> 5月11日上午11:27,siddhu< siddharth .. .. @ gmail.comwrote:
代码片段中的更正..
>On May 11, 11:27 am, siddhu <siddharth....@gmail.comwrote:
A correction in the code snippet..
>>亲爱的专家,
我想创建一个只能在堆上创建对象的类。
我使用了以下方法。
class ss
{
ss(){} <公开:
void * operator new(size_t sz)
{
cout<<<" in new"<<< endl;
return malloc(sz) ; //返回新的字符[sz];
>>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)
{
免费(m);
}
};
int main()
{
ss * s =新ss;
返回0;
}
但是什么时候我做了ss * s = new ss;
编译器抱怨构造函数ss :: ss()是私有的。
因为运算符新函数是类的成员所以编译器不应该
抱怨因为操作员新功能可以调用默认的
构造函数,即使它是私有的。
我很惊讶和困惑。
建议会有很大的帮助。
问候,
Siddharth
>> }
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
这篇关于私有构造函数和新运算符重载。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!