W
Wolfgang
As I understand, a const member function is used by const object to
ensure that its instance isn't modified throughout its life. Am I
missing something..
#include <iostream>
using namespace std;
class A{
public:
A(char& str, size_t sz){
pch = new char [sz];
strncpy(pch, &str, sz);
}
char* print() const {
*pch = 'M';
return pch;
}
~A(){
delete pch;
}
private:
char* pch;
};
int main(){
const char str[] = "I a constant";
const A* obj = new A(*str, sizeof(str));
cout<<"obj->print() = "<<obj->print()<<endl;
return 0;
}
Wg
ensure that its instance isn't modified throughout its life. Am I
missing something..
#include <iostream>
using namespace std;
class A{
public:
A(char& str, size_t sz){
pch = new char [sz];
strncpy(pch, &str, sz);
}
char* print() const {
*pch = 'M';
return pch;
}
~A(){
delete pch;
}
private:
char* pch;
};
int main(){
const char str[] = "I a constant";
const A* obj = new A(*str, sizeof(str));
cout<<"obj->print() = "<<obj->print()<<endl;
return 0;
}
Wg