base-class

Template inheritance and a base member variable

别来无恙 提交于 2019-12-01 21:24:59
问题 I get a weird error when trying to use template inheritance. This is my code: template <class T> class A { public: int a {2}; A(){}; }; template <class T> class B : public A<T> { public: B(): A<T>() {}; void test(){ std::cout << "testing... " << a << std::endl; }; }; And this is the error: error: use of undeclared identifier 'a'; did you mean 'std::uniform_int_distribution<long>::a'? void test(){ std::cout << "testing... " << a << std::endl; } And in case it could affect something I use these

Class base() constructor and pass this

淺唱寂寞╮ 提交于 2019-12-01 21:22:44
I am trying to implement good design patterns for a program I am writing. I have a class structure like this. abstract class SomeBase { public SomeObject obj { get; protected set; } protected SomeBase(SomeObject x) { obj = x; } //Other methods and fields... } public class SomeDerived : SomeBase { public SomeDerived() : base(new SomeObject(this)) { } } Now as I;m sure you now, you can't pass this in a contructor because the object hasn't bee initialized. But I was really hoping there was a workaround. It's not best practice for me to allow SomeDerived() to handle the setting of a base classes

How do we call a virtual method from another method in the base class even when the current instance is of a derived-class?

两盒软妹~` 提交于 2019-12-01 18:41:42
How do we call a virtual method from another method in the base class even when the current instance is of a derived-class? I know we can call Method2 in the Base class from a method in the Derived class by using base.Method2() but what I want to do is calling it from the other virtual method in the Base class. Is it possible? using System; namespace ConsoleApplication1 { class Program { static void Main( string[] args ) { Base b = new Derived( ); b.Method1( ); } } public class Base { public virtual void Method1() { Console.WriteLine("Method1 in Base class."); this.Method2( ); // I want this

Template inheritance and a base member variable

醉酒当歌 提交于 2019-12-01 18:25:52
I get a weird error when trying to use template inheritance. This is my code: template <class T> class A { public: int a {2}; A(){}; }; template <class T> class B : public A<T> { public: B(): A<T>() {}; void test(){ std::cout << "testing... " << a << std::endl; }; }; And this is the error: error: use of undeclared identifier 'a'; did you mean 'std::uniform_int_distribution<long>::a'? void test(){ std::cout << "testing... " << a << std::endl; } And in case it could affect something I use these flags: -Wall -g -std=c++11 I really don't know what is wrong since the same code as pure classes

How do I call a derived class method from the base class?

佐手、 提交于 2019-12-01 02:22:01
I have read several similar questions about this but none seem to solve the problem I am facing. The typical answer is to cast as the derived class but I cannot since I do not know the derived class type. Here is my example: class WireLessDevice { // base class void websocket.parsemessage(); // inserts data into the wireless device object } class WiFi : WireLessDevice { // derived class GPSLoc Loc; } Wireless Device can also be derived to make Bluetooth, Wi-Max, Cellular, etc. devices and thus I do not know which type of wirelessdevice will be receiving the data. When a GPS packet is received

Does a derived class object contain a base class object?

杀马特。学长 韩版系。学妹 提交于 2019-12-01 01:25:56
Consider the following sample code below: #include <iostream> using namespace std; class base { public: base() { cout << "ctor in base class\n"; } }; class derived1 : public base { public: derived1() { cout <<"ctor in derived class\n"; } }; int main() { derived1 d1obj; return 0; } Questions When d1obj is created, the constructors are invoked in the order of derivation : base class constructor is called first and then the derived class constructor. Is this done because of the following reason : In-order to construct the derived class object the base class object needs to be constructed first ?

How do I call a derived class method from the base class?

和自甴很熟 提交于 2019-11-30 21:54:34
问题 I have read several similar questions about this but none seem to solve the problem I am facing. The typical answer is to cast as the derived class but I cannot since I do not know the derived class type. Here is my example: class WireLessDevice { // base class void websocket.parsemessage(); // inserts data into the wireless device object } class WiFi : WireLessDevice { // derived class GPSLoc Loc; } Wireless Device can also be derived to make Bluetooth, Wi-Max, Cellular, etc. devices and

Call a C++ base class method automatically

天大地大妈咪最大 提交于 2019-11-30 18:31:16
I'm trying to implement the command design pattern , but I'm stumbling accross a conceptual problem. Let's say you have a base class and a few subclasses like in the example below: class Command : public boost::noncopyable { virtual ResultType operator()()=0; //Restores the model state as it was before command's execution. virtual void undo()=0; //Registers this command on the command stack. void register(); }; class SomeCommand : public Command { virtual ResultType operator()(); // Implementation doesn't really matter here virtual void undo(); // Same }; The thing is, everytime operator () is

C++: Accessing parent methods and variables

守給你的承諾、 提交于 2019-11-30 18:13:09
In which way should I access this parent method and parent variable? class Base { public: std::string mWords; Base() { mWords = "blahblahblah" } }; class Foundation { public: Write( std::string text ) { std::cout << text; } }; class Child : public Base, public Foundation { DoSomething() { this->Write( this->mWords ); // or Foundation::Write( Base::mWords ); } }; Thanks. Edit: And what if there is ambiguity? The two syntaxes you use in your code ( this->... and qualified names) are only necessary specifically when there is ambiguity or some other name lookup issue, like name hiding, template

Can a Base Class Method return the type of the derived class?

懵懂的女人 提交于 2019-11-30 13:23:34
Based on the other posts I have read, it seems that this may not be possible, but I thought I would post what I am trying to do and see if anyone knows of a solution. I am trying to add a "Clone()" method to classes generated from a Telerik Open Access domain model. No problem. I was able to figure out how to add a base class to the generated entity models so that I could identify those classes by their base class. ( All entities inherit from a base class ) I want ALL these entity model classes to be able to Clone themselves. I have found a solution for that as well. ( Deep Cloning Objects )