|
Say I have something like -- this is just an example mind you.
class Car { void accelerate(); void stop(); } class Person { void drive(Car car); } class Toyota : public Car { void accelerateUncontrollably(); } class ToyotaDriver : public Person { void...
Started by DevDevDev on
, 7 posts
by 7 people.
Answer Snippets (Read the full thread at stackoverflow):
But ask yourself accelerateUncontrollably(); } class Toyota : public Car : IToyotaAccelerable { void accelerateUncontrollably(); } class ToyotaDriver....
You can, of course, make a cast...
And then the declaration should be in the Car class.
|
|
Class Base { public: virtual void foo() {} }; class Derived: public Base { public: virtual void foo() {} }; int main() { Base *pBase = NULL; Base objBase; Derived objDerived; pBase = &objDerived; pBase->foo(); /*Here Derived class foo will be called...
Started by coolcake on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
PBase->Base::foo()
You can do it through scope resolution operator ::
Something like this:
pBase->Base::foo()
Both responses above are correct...But be careful, if you need to do that, maybe you have a big problem about the conception or the design... .
|
|
Can I force a parent class to call a derived class's version of a function?
class Base(object): attr1 = '' attr2 = '' def virtual(self): pass # doesn't do anything in the parent class def func(self): print "%s, %s" % (self.attr1, self.attr2) self.virtual...
Started by Paul on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
# do stuffIf you instantiate ....
Virtual(self): pass def func(self, cb): print "%s, %s" % (self.attr1, self.attr2) cb() class Derived(Base): attr1 = 'I am in class Derived' attr2 = 'blah blah' def virtual(self): # do stuff...
|
Ask your Facebook Friends
|
The pointer of derived class returned by new can be type cast to the pointer of its base class.
Is this true or false?
I know dynamic_cast can be used to cast downside. Generally, how to cast a pointer of derived class to a pointer of its base class?
Started by skydoor on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
This is the whole point of polymorphism: An instance of a derived class should always be safely usable as an instance ....
Conversion from a pointer to a derived class to a pointer to a base class is implicit.
|
|
I know that the derived class can't access the private members of the base class, so why does the derived class inherit the private members of the base class? Is there any case that it is useful?
Thanks!
Started by skydoor on
, 9 posts
by 9 people.
Answer Snippets (Read the full thread at stackoverflow):
A derived class....
An instance of the derived class contains instances.
The derived class doesn't "inherit" the private members of the base class in any way - it can't access them, so it doesn't "inherit" them.
|
|
From an OOP point of view is there any difference between a derived class and an inherited class? Or is it simply terminology?
Started by JL on
, 6 posts
by 6 people.
Answer Snippets (Read the full thread at stackoverflow):
The term derived class is preferred C++ parlance for a class that inherits derives Base Class (Sub Class is a derived class of Base Class) and ....
Is called the derived class.
|
|
I have a problem and not so sure how to solve it..
Consider the class:
public class BaseClass<T> { public T PreviousInstance { get; set; } }
Now my secondary data class:
public class DataClass : BaseClass<DataClass> { public bool ABoolProperty...
Started by Gavin Uttley on
, 7 posts
by 7 people.
Answer Snippets (Read the full thread at stackoverflow):
What you can do is carrying over....
Of "I can use an instance of a derived class anywhere that an instance of a base class is requiredHey,
You could try shadowing the property as in the following:
public class DerivedDataClass.
|
|
I have a generic class in my project with derived classes.
public class GenericClass <T> : GenericInterface<T> { } public class Test : GenericClass <SomeType> { }
Is there any way to find out if a Type object is derived from the GenericClass...
Started by bernhardrusch on
, 6 posts
by 6 people.
Answer Snippets (Read the full thread at stackoverflow):
GenericClass implementation inherits from an abstract non-generic base class such as GenericClassBase, you.
|
|
I have a derived class that adds only methods to a base class. How can serialize the derived class so that it matches the serialization of the base class? i.e. The serialized xml of the derived class should look like:
<BaseClass> ... </BaseClass...
Started by Chris Herring on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
I haven't tried it but can you not just cast it ?
serializer(stream, (BaseClass)derived);
Edit
Also, if you want to have a single XmlSerialiser....
Here's a sample topic and similar problem resolved by interface mentioned .
derived class.
|
|
Hi,
Is it possible to access a base class function which has the same signature as that of a derived class function using a derived class object?. here's a sample of what I'm stating below..
class base1 { public: void test() {cout<<"base1"<<...
Started by Karthik on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
class virtual void test(); in your base class.
Test()
You cant override a method in derived class if you dint provide a virtual key word..
|