D
Dennis Jones
Hello,
I have a hierarchy of classes in which there will be a data element that is
common to all descendant classes. This element (a string in this case) is
constant, but specific to each class. So I have something like this:
class Base
{
private:
virtual std::string GetString() const = 0;
public:
void SomeFunction() { GetString(); }
};
class Derived : public Base
{
private:
const std::string FString;
virtual std::string GetString() const { return FString; }
public:
Derived()
: Base(),
FString( "Derived" ) {}
};
This isn't completely unreasonable, but I do have to duplicate the 'FString'
data element and its getter function in every derived class. It would be
much nicer if I could do something like this instead:
class Base
{
private:
const std::string FString;
std::string GetString() const { return FString; }
public:
void SomeFunction() { GetString(); }
};
class Derived : public Base
{
public:
Derived()
: Base(),
FString( "Derived" ) {}
};
However, this won't work because the compiler will complain that "'FString'
is not an unabiguous base class of 'Derived'" which, of course, is true and
makes perfect sense. A less desirable (but still reasonable) solution would
be to initialize 'FString' in the constructor's body:
class Derived : public Base
{
public:
Derived()
: Base()
{
FString = "Derived";
}
};
But of course, this won't work either because 'FString' is const! What's
one to do? Is there a way to eliminate the unnecessary duplication and
maintain the constness of the data element at the same time?
Thanks,
- Dennis
I have a hierarchy of classes in which there will be a data element that is
common to all descendant classes. This element (a string in this case) is
constant, but specific to each class. So I have something like this:
class Base
{
private:
virtual std::string GetString() const = 0;
public:
void SomeFunction() { GetString(); }
};
class Derived : public Base
{
private:
const std::string FString;
virtual std::string GetString() const { return FString; }
public:
Derived()
: Base(),
FString( "Derived" ) {}
};
This isn't completely unreasonable, but I do have to duplicate the 'FString'
data element and its getter function in every derived class. It would be
much nicer if I could do something like this instead:
class Base
{
private:
const std::string FString;
std::string GetString() const { return FString; }
public:
void SomeFunction() { GetString(); }
};
class Derived : public Base
{
public:
Derived()
: Base(),
FString( "Derived" ) {}
};
However, this won't work because the compiler will complain that "'FString'
is not an unabiguous base class of 'Derived'" which, of course, is true and
makes perfect sense. A less desirable (but still reasonable) solution would
be to initialize 'FString' in the constructor's body:
class Derived : public Base
{
public:
Derived()
: Base()
{
FString = "Derived";
}
};
But of course, this won't work either because 'FString' is const! What's
one to do? Is there a way to eliminate the unnecessary duplication and
maintain the constness of the data element at the same time?
Thanks,
- Dennis