在通话时挑选const与非const成员函数 [英] Picking const vs. non-const member functions upon a call
问题描述
大家好,
请在下面的代码中查看问题...
谢谢!
Dave
#include< iostream>
使用命名空间std;
class foo
{
public:
foo(int d):data(d){}
void print()const {cout<< " const的" << endl;}
void print(){cout<< "非const" << endl;}
私人:
int数据;
};
int main()
{
foo a(42);
//下面的调用调用非const打印();
//我想要调用const print()!
// a.print();
//这是一种方法。是否有更优雅的方式?
//制作const不在我的问题范围内!
static_cast< const foo>(a).print();
返回0;
}
Hello all,
Please see the question in the code below...
Thanks!
Dave
#include <iostream>
using namespace std;
class foo
{
public:
foo(int d) : data(d) {}
void print() const {cout << "const" << endl;}
void print() {cout << "non-const" << endl;}
private:
int data;
};
int main()
{
foo a(42);
// The call below calls the non-const print();
// I want the const print() to be called!
// a.print();
// Here''s one way to do it. Is there a more elegant way?
// Making a const is not within the scope of my question!
static_cast<const foo>(a).print();
return 0;
}
推荐答案
cheeser写道:
cheeser wrote:
#include< iostream>
class foo {
私人:
int data;
public:
foo(int d):data(d){}
void print(void)const {std :: cout<< " const的" << std :: endl;}
void print(void){std :: cout<< "非const" << std :: endl;}
};
int main(int argc,char * argv []){
const foo a(42);
a。 print();
返回0;
}
#include <iostream>
class foo {
private:
int data;
public:
foo(int d): data(d) { }
void print(void) const {std::cout << "const" << std::endl;}
void print(void) {std::cout << "non-const" << std::endl;}
};
int main(int argc, char* argv[]) {
const foo a(42);
a.print();
return 0;
}
" cheeser" < CH ********** @ yahoo.com>在消息中写道
news:vo ************ @ news.supernews.com ...
"cheeser" <ch**********@yahoo.com> wrote in message
news:vo************@news.supernews.com...
大家好,
请在下面的代码中查看问题...
谢谢!
Dave
#include< iostream>
使用命名空间std;
class foo
{
公开:
foo(int d):data(d){}
void print()const {cout<< " const的" << endl;}
void print(){cout<< "非const" <<私有:
int数据;
};
int main()
{
foo a( 42);
//下面的调用调用非const print();
//我希望调用const print()!
使用const对象调用函数。
const foo a(42);
// a.print();
//这是一种方法。有更优雅的方式吗?
定义优雅。
//制作const不在我的问题范围内!
你问的是const成员函数,所以是的,
你是否否认它。 :-)
static_cast< const foo>(a).print();
返回0;
}
Hello all,
Please see the question in the code below...
Thanks!
Dave
#include <iostream>
using namespace std;
class foo
{
public:
foo(int d) : data(d) {}
void print() const {cout << "const" << endl;}
void print() {cout << "non-const" << endl;}
private:
int data;
};
int main()
{
foo a(42);
// The call below calls the non-const print();
// I want the const print() to be called!
The invoke the function with a const object.
const foo a(42);
// a.print();
// Here''s one way to do it. Is there a more elegant way?
Define "elegant".
// Making a const is not within the scope of my question!
You''re asking about const member functions, so yes it is,
whether you deny it or not. :-)
static_cast<const foo>(a).print();
return 0;
}
你想解决什么真正的具体问题?
-Mike
What real specific problem are you trying to solve?
-Mike
>请在下面的代码中查看问题...
> Please see the question in the code below...
谢谢!
Dave
#include< iostream>
using namespace std;
你确定要的吗?
class foo
{
公开:
foo(int d): data(d){}
void print()const {cout<< " const的" << endl;}
这将被调用
const foo f;
f.print();
void print(){cout<< "非const" << endl;}
这将被调用
foo f;
f.print();
private:
int data;
};
int main()
{
foo a(42);
//下面的调用调用非const print();
//我想调用const print()!
// a.print() ;
//这是一种方法。是否有更优雅的方式?
//制作const不在我的问题范围内!
static_cast< const foo>(a).print();
Thanks!
Dave
#include <iostream>
using namespace std;
Are you sure you want that?
class foo
{
public:
foo(int d) : data(d) {}
void print() const {cout << "const" << endl;}
This would be called with
const foo f;
f.print();
void print() {cout << "non-const" << endl;}
And that would be called with
foo f;
f.print();
private:
int data;
};
int main()
{
foo a(42);
// The call below calls the non-const print();
// I want the const print() to be called!
// a.print();
// Here''s one way to do it. Is there a more elegant way?
// Making a const is not within the scope of my question!
static_cast<const foo>(a).print();
是否调用const成员函数取决于对象的constness上的
。你为什么这么想?也许
我们可以找到另一种方式。
Jonathan
Whether a const member function will be called or not depends
on the constness of the object. Why do you want that? Perhaps
we could find another way.
Jonathan
这篇关于在通话时挑选const与非const成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!