Override member field in derived classes
The new b
added in the derived class doesn't override base's b
. It just hides it.
So, in the derived class you have two b
and the virtual method prints corresponding b
.
C++ Override base member value
B::foo
is a completely separate member from A::foo
. That is why initializing B::foo
does not update A::foo
. You would have to do something more like this instead:
class
{
public:
int foo;
A(int value = 0) : foo(value) {}
};
class B : public A
{
public:
B() : A(1) {}
};
int main()
{
A *a = new B();
std::cout << a->foo;
delete a;
std::getchar();
return 0;
}
Override a data member in a C++ derived class
You could do something like this (but requires C++11):
#include <type_traits>
// A way to check for 'attr' data member presence/absence using SFINAE
template<class T> constexpr auto missing_attr(T) -> decltype(T::attr, bool())
{
return false;
}
constexpr bool missing_attr(...) { return true; }
struct Base { /* There might be (or not) a float data member 'attr' */ };
template<bool> struct Inject;
template<> struct Inject<true> { float attr = 0.0; };
template<> struct Inject<false> {};
struct Derived : public Base, protected Inject<missing_attr(Base())>
{
void do_something_with_attr()
{
// Derived has only one 'float attr' (it can be from Base or from Inject).
a *= a;
}
};
There are other ways to detect if a data member exists, e.g. the member detector idiom (C++03 compatible).
Overriding member variables in C++
You printed i
via A
's (the parent class's) print
function. Your class has two i
s, and A
's print
only knows where to find one of them (the one from A
). If you want to print B
's i
, it has to be done in a B
method (that knows where to find it).
You could make a virtual
accessor method on A
that provides access to i
, and have B
override it, while A
's print
uses it (rather than direct accessing i
), so it can see the i
from B
, but that's the best you'll get. It works because methods can be virtual
(looked up dynamically based on runtime type), and if the accessor is virtual on A
and overridden on B
, the B
method will be found when A
's print
calls it, and B
's method sees the definitions in B
.
C++ override a member variable
By declaring TSubclassof<AWeapon> ItemClass;
in the derived class, you are basically hiding ItemClass
in the base class. So, it will look like you "changed" the type, but you didn't, only in the derived class will you see the new type variable.
If you call a member function of the base class, which accesses ItemClass
, it will use the "old" type variable.
If not, is it possible to hide a variable in a child class? i.e. I could hide ItemClass and create a new variable called WeaponClass.
That's possible, you could declare ItemClass
as a private
variable, so that the derived class doesn't have access to it. Then when you can create WeaponClass
, without there being a ItemClass
in the derived class.
How do I override a field in a base class with a property in a derived class?
You're trying to access the super's member
as if it were a class attribute. Try with:
class Derived(Base):
@property
def member(self):
print "intercepting getter"
return self._member
@member.setter
def member(self, value):
print "intercepting setter"
self._member = value
Override initial value of a member variable of base class
What is the correct way to implement the following?
Without meaning to sound difficult, it really depends on exactly what you want to achieve.
I'm going to make a guess that we're simply be asking an object what type it is, regardless of which interface we're calling on:
struct Base {
virtual ~Base() = default;
virtual const std::string id() const {
return "Base";
}
};
struct Derived : Base {
virtual const std::string id() const override {
return "Derived";
}
};
Here's another way:
struct Base {
virtual Base(std::string ident = "Base")
: _id(std::move(ident))
{}
virtual ~Base() = default;
std::string& id() const {
return _id;
}
private:
std::string _id;
};
struct Derived : Base {
Derived() : Base("Derived") {}
};
And another, using value is interface, but note that this will disable the assignment operator
struct Base {
virtual Base(std::string ident = "Base")
: id(std::move(ident))
{}
virtual ~Base() = default;
const std::string id;
};
struct Derived : Base {
Derived() : Base("Derived") {}
};
This list is by no means exhaustive.
Related Topics
Why Does the Most Negative Int Value Cause an Error About Ambiguous Function Overloads
How to Return in Void Function
Avoiding the Tedium of Optional Parameters
Determine Process Info Programmatically in Darwin/Osx
I Want to Convert Std::String into a Const Wchar_T *
Most Efficient/Elegant Way to Clip a Number
Does This Really Break Strict-Aliasing Rules
Why Is Modifying a String Through a Retrieved Pointer to Its Data Not Allowed
Why Do Functions Need to Be Declared Before They Are Used
Compilation Error: "Stddef.H: No Such File or Directory"
Is a Union Member's Destructor Called
Is There a Use for Function Declarations Inside Functions