derived-class

c++ , pthread and static callbacks. “this” returns a pointer to the base class inctead of the derived one (part 2)

╄→尐↘猪︶ㄣ 提交于 2019-12-02 07:20:40
问题 this thread was started here but due to lack of an altogether good example (and in order to avoid delete all that question) it is re-written here. So, in the following example, the void cppthread::ThreadedFunc() gets spawned to execute as a separate thread . Instead I would prefer void ThreadedWrite::ThreadedFunc() to be executed. How can I do that? (some more details follow after the code) cppthread.hpp #ifndef CPPTHREAD_HPP #define CPPTHREAD_HPP #include <pthread.h> using namespace std;

c++ , pthread and static callbacks. “this” returns a pointer to the base class inctead of the derived one (part 2)

[亡魂溺海] 提交于 2019-12-02 07:18:18
this thread was started here but due to lack of an altogether good example (and in order to avoid delete all that question) it is re-written here. So, in the following example, the void cppthread::ThreadedFunc() gets spawned to execute as a separate thread . Instead I would prefer void ThreadedWrite::ThreadedFunc() to be executed. How can I do that? (some more details follow after the code) cppthread.hpp #ifndef CPPTHREAD_HPP #define CPPTHREAD_HPP #include <pthread.h> using namespace std; class cppthread { public: cppthread(); virtual ~cppthread(); virtual void threadedFunc(); ///parentObj (ie

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

Simpler “Preventing derived classes” in C++

流过昼夜 提交于 2019-12-01 17:17:27
Going under the assumption that there is a legitimate reason for preventing derivation from some class, Bjarne gives a solution here for the answer to "Can I stop people deriving from my class?" However, I thought of: class final { protected: final() { } // line 3 }; class B : private virtual final { }; class D : public B { // line 9 }; int main() { B b; D d; // line 14 } When trying to compile, one gets: foo.cpp: In constructor ‘D::D()’: foo.cpp:3: error: ‘final::final()’ is protected foo.cpp:9: error: within this context foo.cpp: In function ‘int main()’: foo.cpp:14: note: synthesized method

C++ Initialize base class' const int in derived class?

六眼飞鱼酱① 提交于 2019-12-01 16:34:10
I have a constant int variable in my base class, and I would like to initialize responding one in my derived class, with different value (taken as a parameter), is this possible? Here's what I did: // Base.h (methods implemented in Base.cpp in the actual code) class Base { public: Base(const int index) : m_index(index) {} int getIndex() const { return m_index; } private: const int m_index; }; // Derived.h class Derived : public Base { public: Derived(const int index, const std::string name) : m_name(name) {} void setName(const std::string name) { m_name = name; } std::string getName() const {

C++ Initialize base class' const int in derived class?

有些话、适合烂在心里 提交于 2019-12-01 15:31:15
问题 I have a constant int variable in my base class, and I would like to initialize responding one in my derived class, with different value (taken as a parameter), is this possible? Here's what I did: // Base.h (methods implemented in Base.cpp in the actual code) class Base { public: Base(const int index) : m_index(index) {} int getIndex() const { return m_index; } private: const int m_index; }; // Derived.h class Derived : public Base { public: Derived(const int index, const std::string name) :

std::bind()-ing a base protected member function from a derived class's member function

落爺英雄遲暮 提交于 2019-12-01 14:21:21
问题 I want to bind() to my base class's version of a function from the derived class. The function is marked protected in the base. When I do so, the code compiles happily in Clang (Apple LLVM Compiler 4.1) but gives an error in both g++ 4.7.2 and in Visual Studio 2010. The error is along the lines of: "'Base::foo' : cannot access protected member." The implication is that the context for the reference is actually within bind() , where of course the function is seen as protected. But shouldn't

Why can't we use a constructor with parameter in derived classes

寵の児 提交于 2019-12-01 13:04:35
Why is this not possible? I get the following compiler-error when instantiating "DerivedClass" with a constructor-parameter: 'GenericParameterizedConstructor.DerivedClass' does not contain a constructor that takes 1 argument But calling a very similar method works. Why? class Program { static void Main(string[] args) { // This one produces a compile error // DerivedClass cls = new DerivedClass("Some value"); // This one works; DerivedClass cls2 = new DerivedClass(); cls2.SomeMethod("Some value"); } } public class BaseClass<T> { internal T Value; public BaseClass() { } public BaseClass(T value)

Is there no way to upcast into an abstract class and not modify it each time a class is derived from it?

北城余情 提交于 2019-12-01 06:17:33
问题 #include<iostream> using namespace std; class Abs { public: virtual void hi()=0; }; class B:public Abs { public: void hi() {cout<<"B Hi"<<endl;} void bye() {cout<<"B Bye"<<endl;} }; class C:public Abs { public: void hi() {cout<<"C Hi"<<endl;} void sayonara() {cout<<"C Sayonara"<<endl;} }; int main() { Abs *bb=new B; bb->bye(); Abs *cc=new C; cc->sayonara(); }//main The compiler says test2.cpp: In function ‘int main()’: test2.cpp:26: error: ‘class Abs’ has no member named ‘bye’ test2.cpp:28: