const at the end of function and passing const object in a function call
2009-01-23 13:45
621 查看
#include <ostream.h>
Class B
{
public:
virtual void print() const
{
cout << "printB" << std::endl;
}
};
class A
{
public:
virtual void print(const B* b) const
{
cout << "printA" << std::endl;
b->print();
}
};
main()
{
cout << "test" << std::endl;
A* a;
B* b;
a = new A();
b = new B();
a->print(b);
}
Note:
1. the const at the end of the function declaration: indicates that the "this" parameter to the function is const.
2. use the const because it don't modify the object.
Class B
{
public:
virtual void print() const
{
cout << "printB" << std::endl;
}
};
class A
{
public:
virtual void print(const B* b) const
{
cout << "printA" << std::endl;
b->print();
}
};
main()
{
cout << "test" << std::endl;
A* a;
B* b;
a = new A();
b = new B();
a->print(b);
}
Note:
1. the const at the end of the function declaration: indicates that the "this" parameter to the function is const.
2. use the const because it don't modify the object.
相关文章推荐
- General Object Initiating Function by the Example of $injector.instantiate Implementation in angular
- [Javascript] A function works like 'print_r()' in PHP to print out the details of an object for JS debugging
- Given constant integers x and t, write a function that takes no argument and returns true if the function has been called x number of times in last t secs.
- This function has none of Deterministic,no sql,or reads sql data in its declaration and binary logging is enabled(you *might* want to use the less safe log_bin_trust_function_creators variable
- Odd elements at odd and even elements at even positionThis is question asked in one of the interview
- 【JOURNAL】deep sadness and sorrow at the unexpected passing of Michael Jackson
- 解决 ECMall 错误 Call to a member function get() on a non-object in frontend.base.php
- The usage of the Javascript method call and apply(what is the new and object.create difference.)(What does call(null) mean?)
- Relationship of the this pointer and (non-)const member function
- error -27257: Pending web_reg_save_param/reg_find/create_html_param[_ex] request(s) detected and reset at the end of iteration number 1
- The difference of static, readonly and const in C#
- by the end of, at the end of , in the end , to the end 的区别与用法
- XML文件要有根标签(Extra content at the end of the document in file 错误)
- what are the semantics of a const member function
- A const object can only call const function
- GSR :the meaning of <load sharing elements> <refernce><refcount> in CEF and how to calculate them
- Such Summarize at the end of University .
- The function of include in Verilog
- An error occurred at line: 307 in the generated java file The code of method _jspService (HttpServle
- Swap kth element from the beginning and kth element from the end of linked list -- Amazon