S
Shea Martin
I have a MyString class:
class MyString
{
public:
MyString() {}
MyString(const char *str)
{
_buffer = new char[strlen(str)+1];
strcpy(_buffer, str);
}
void func1(const MyString &str)
{
printf("str is %s\n", str._buffer);
}
private:
char *_buffer;
};
Passing func1 a const char *, will result in a deep copy occurring. Because str
is const, the deep copy is not necessary. Obviously I can't have my conversion
constructor do a shallow copy, unless I know the created object is const.
It there a way to differentiate whether a constructor is creating a const object
or not?
i.e.,
class MyString
{
public:
MyString() {}
MyString(const char *str)
{
_buffer = new char[strlen(str)+1];
strcpy(_buffer, str);
}
const MyString(const char *str)
{
_buffer = (char*)str;
}
void func1(const MyString &str)
{
printf("str is %s\n", str._buffer);
}
private:
char *_buffer;
};
Obviously this syntax won't work, but you get the idea of what I am trying to
accomplish.
Thanks,
~Shea M.
class MyString
{
public:
MyString() {}
MyString(const char *str)
{
_buffer = new char[strlen(str)+1];
strcpy(_buffer, str);
}
void func1(const MyString &str)
{
printf("str is %s\n", str._buffer);
}
private:
char *_buffer;
};
Passing func1 a const char *, will result in a deep copy occurring. Because str
is const, the deep copy is not necessary. Obviously I can't have my conversion
constructor do a shallow copy, unless I know the created object is const.
It there a way to differentiate whether a constructor is creating a const object
or not?
i.e.,
class MyString
{
public:
MyString() {}
MyString(const char *str)
{
_buffer = new char[strlen(str)+1];
strcpy(_buffer, str);
}
const MyString(const char *str)
{
_buffer = (char*)str;
}
void func1(const MyString &str)
{
printf("str is %s\n", str._buffer);
}
private:
char *_buffer;
};
Obviously this syntax won't work, but you get the idea of what I am trying to
accomplish.
Thanks,
~Shea M.