I have inhereted CAsyncSocket and wanted to pass the objects around.
class ClientSocket : public CAsyncSocket
{
CAsyncSocket nitSocket;
public:
ClientSocket(void);
virtual ~ClientSocket(void);
};
I get sevaral compile errors when i do
void SomeOtherClass::func(ClientSocket &socket)
this->socket = socket;
}
Error:
'CAsyncSocket::operator =' : cannot access private member declared in class 'CAsyncSocket'
I looked into file and found
private:
CAsyncSocket(const CAsyncSocket& rSrc); // no implementation
void operator=(const CAsyncSocket& rSrc); // no implementation
Should i make my copy constructor but since there is no implementation for base class would my code crash at runtime.
Important: Should i make a copy ? WOULD my new object receive the events of original object?
Polymorphic types in C++ are usually made non-copyable because taking a copy of base class easily leads to slicing.
Related
I have an abstract class that I have derived from two child classes. One of them has a protected variable that the other one does not. To make the code more general, I have to use an smart pointer of the abstract class. Is there any way to access the protected variable through the pointer? As an example, Consider the following code (the real code is huge and I had to write this sample code):
class Pen{
public:
pen(string _color): color(_color){};
getColor(){return color;};
protected:
string color;
};
// base abstract class
class writer{
public:
writer() {}
virtual changeColor(string color) = 0;
};
class oldWriter: public writer{
protected:
Pen *pen;
public:
oldWriter(string _pen):
pen(_pen){}
virtual changeColor(string color){ pen->color = color;};
};
class youngWriter: public writer{
protected:
Pen *pen;
Pencile pencil; //we need to have access to pencil
public:
youngWriter(string _pen):
pen(_pen){}
virtual changeColor(string color){ pen->color = color;};
Pencil getPencil(){return pencil;};
};
int main(){
unique_ptr<Writer> artist;
Pencil pencil = artist->getPencil(); //how?
}
How can we access "pencil" in "youngWriter" class through "artist"?
if i well understood the code, the problem is related to class "Writer" which haven't a method called getPencil.
getPencil is a method of class youngWriter.
Therefore:
artist have to be a youngWriter instance.
Writer have to contains a method getPencil abstract.
I am trying to create a shared pointer of a singleton instance. Below is the way I am trying to do it.
Singleton implementation:
class MessageQueue
{
private:
std::mutex _mutex;
std::queue<std::string> _messageQueue;
MessageQueue() {}
public:
MessageQueue(const MessageQueue& obj) = delete;
MessageQueue& operator=(const MessageQueue& obj) = delete;
static MessageQueue& getInstance()
{
static MessageQueue instance;
return instance;
}
void pushMessage(std::string& message);
std::string popMessage();
void flushOut();
};
Using singleton instance to create a shared pointer:
class Producer
{
private:
std::shared_ptr<messagequeue::MessageQueue> _queue;
public:
Producer()
{
if(!_queue)
{
auto& instance = messageq::MessageQueue::getInstance();
_queue = std::make_shared<messageq::MessageQueue>(instance);
}
}
Producer(const Producer& obj);
Producer& operator=(const Producer& obj);
};
Now, the problem is with this much, the code doesn't compile because the copy constructor of the singleton class is deleted. If, I define the copy constructor, it does get compiled, but then it's not much of a singleton class anymore now then.
So, what is the best way to achieve this? My objective is that I want to make a shared_ptr instance variable of a class instance and the shared resource would be a singleton object.
Please help here.
If you ensure that you create the queue first and only after the producers and consumers, so that they get destroyed first, you don't need a smart pointer to the queue. Producer and consumers can just keep a plain pointer to the queue in this case.
Shared ownership saves you from thinking about who owns what and their lifetimes, however, that leads to poor designs in 99% of cases.
I have a base class (with which I want to simulate interfaces)
template<typename TType>
class Base
{
public:
virtual SomeTemplatedClass<TType> GetTheObject() = 0;
}
and obviously a derived class
template<typename TType>
class Derived : public Base<TType>
{
public:
virtual SomeTemplatedClass<TType> GetTheObject() = 0;
}
but for some specific type I have the intention to specialize the 'GetTheObject'
template<>
SomeTemplatedClass<int> Derived<int>::GetTheObject()
{
return 5;
}
Visual Studio 2015 complains it cannot instantiate abstract class, when I try to use
Derived<int>
Providing even a throwing behavior to a template version
class Derived : public Base<TType>
{
public:
virtual SomeTemplatedClass<TType> GetTheObject() override
{
throw <something>;
}
}
Let everything compile.
So my question is: Why do i need to provide a generic behavior, when I have a specific one and the only one that is needed?
You don't need to implement the generic GetTheObject, but you need to declare it as non-pure. Otherwise your class is abstract.
template<typename TType>
class Derived : public Base<TType>
{
public:
virtual SomeTemplatedClass<TType> GetTheObject();
}
You can specialise the function now.
You won't be able to instantiate any non-specialised derived objects (you will get linker errors).
You cannot make an abstract class into concrete by simply providing an implementation of its pure virtual member outside of the class.
class A { virtual void f() = 0; }; // A is abstract
void A::f() {} // A is still abstract
Templates are no different.
template <int> class A { virtual void f() = 0; }; // A is abstract
template <int k> void A<k>::f() {} // A is still abstract
A function specialisation changes nothing.
template <int> class A { virtual void f() = 0; }; // A is abstract
template <int k> void A<k>::f() {} // A is still abstract
template <> void A<42>::f() {} // srsly are you kidding?
If you want the generic case to be abstract and the specialised case concrete, you need to specialise the entire class, not just the pure function implementation.
Using C++ pipes api(1.2.0), How can I possibly get a call in Mapper.cleanup() after the map() phase of the mapper? Basically for each chunk I want to store my records in-memory during the map phase and then apply some processing afterwards.
Any hints are welcome,
Thanks,
The Mapper c++ class extends Closable:
class Mapper: public Closable {
public:
virtual void map(MapContext& context) = 0;
};
and Closable has the following signature:
class Closable {
public:
virtual void close() {}
virtual ~Closable() {}
};
So (not being a c++ programmer), i'm guessing you just need to write your logic in a method named close
if i have a static method Only advantage is that we have single copy.Need not have a object to call the Method. The same can be done be creating an object i.e we can call method with object. Why should we have static method. Can someone provide a example to explain?
Static methods can be useful when you have private constructors, because you want to abstract the instantiation process.
For example in C++:
class Foo {
Foo() {}
public:
static Foo *create() {
return new Foo;
}
};
In that example the abstraction just called an otherwise in accessible constructor, but in practice you might want to have a pool of objects which is shared and so the create() method would be managing this for you.
Sometimes when you have const members which need to be initalised at construction time it can be cleaner to move the logic for this into a private static method, e.g.:
struct Foo;
struct Bar {
Bar() : f(make()) {
}
private:
const Foo f;
static Foo make() {
// Create it here
}
};
The static method is used when developer is really sure the method is only have one instance in the class. There are no other instance that can change that.
eg :
public class People
{
private
public static Int32 GetValue(Int x)
{
return x + 3;
}
}
So even you are make instances of object people, the return from getvalue static method only produce x + 3.
It is usually used when you are really sure to make a functional method like math or physics method.
You can refer to functional programming that using static point of view.
Some of the old school guys are overusing the static method instead of doing OOP approach.
eg:
public class People
{
public static DataSet GetPeopleById(String personId)
{ .... implementation that using SQL query or stored procedure and return dataset ... }
public static DataSet GetXXXXXXX(String name, DateTime datex)
{ .... implementation ... }
}
The implementation above can be thousands of lines
This style happens everywhere to make it like OOP style (because it happen in the class) but thinking like procedural approach.
This is a help since not all people understand OOP style rather than like OOP style.
The other advantage using static are saving memory footprints and faster.
You can see in the blogs : http://www.dotnetperls.com/callvirt