C++ Generic use of parent class (reference or pointer) [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
class FunctionBase {
public:
const double operator() (double a_) const = 0;
}
class AddN : public FunctionBase {
public:
AddN (int n_) : FunctionBase(), _n(n_) {}
const double operator() (double a_) const { return (a_ + n); }
private:
int _n;
}
Do I have to use FunctionBase * as a placeholder because of the inability of the compiler to reconstruct a AddN from a FunctionBase, or is there a way to use FunctionBase &?
-- EDIT --
I have a std::map<std::string, FunctionBase *>, and I'm curious if I'm able to use a reference instead of a pointer so I can guarantee the pointer is not NULL before I use it. I'm trying to make the code more error resistant. If I switch it to a FunctionBase &, the compiler complains that it cannot instantiate AddN from FunctionBase & which makes total sense, however I was hoping there was a common workaround I was just unaware of.

Instead of defining a class hierarchy suggest using std::function, or boost::function. These can hold a callable and can be stored in the map by value, guaranteeing the function object lifetime:
std::map<std::string, std::function<double(double)>> functions;
This eliminates the need for using a class hierarchy and does not require that the functions added to the map are related in anyway, which is more flexible.
Just to point out that using a reference instead of a pointer does not guarantee that the object being referred to still exists. Dangling references are possible, just as dangling pointers are.

Related

How can I pass to a method an argument that could be a function of any type? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have to make a library as an assignment that would implement a menu-like functionality on a linux terminal. So I have to for each submenu enable a position to be either another submenu or a call to some function.
I was wondering how to implement such a call. Since it is to be a library, it could be used to a dozen of different kinds of functions, taking different number and types of arguments.
How to implement such a method calling another functions that would not assume anything about those functions any yet could call them? Should I use templates somehow or is there another method for that; or maybe I should implement it in a whole other way?
You can use objects with virtual methods to do this. Then the application can decide, which parameters to put into the callback object.
class MenuItem;
// Callback Interface
struct MenuCallback
{
virtual void activated(MenuItem* sender) = 0;
virtual ~MenuCallback(){}
};
// Application Code
struct ConcretMenuCallback: MenuCallback
{
ConcretMenuCallback(int parameter1);
void activated(MenuItem* sender)
{
// do something with the data stored in this object
}
};
// Example Item
class MenuItem
{
MenuItem(MenuItem*parent, std::string caption, MenuCallback* cb);
void notify();
}
MenuItem::notify()
{
// call the callback-Object
if(m_callback)
m_callback->activated(this);
}

Implement several order relations for one class? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Which is the best way to implement several order relations for only one class? I have an idea with the Strategy Pattern but I'm not sure that's a good idea. And if there is not a best way, why?
Create a functor class and initialize a member with the order relation you want to use. Have operator() use the member to decide the ordering of the two operands.
class Ordering
{
public:
Ordering(int method) : _method(method) {}
bool operator()(const MyObject & first, const MoObject & second) const
{
switch(_method)
{
case 0:
return first.name < second.name;
case 1:
return first.age < second.age;
// ...
}
}
int _method; // an enum would be better
};
std::sort(myobjs.begin(), myobjs.end(), Ordering(selected_method));
I think Strategy is a better way here, and I'm not pretty sure that a switch structure is a good idea (imagine, 1000 comparison methods in one switch... Too heavy, isn't it?)
So let A, a class which need a method comparison.
I suggest to create one class per method, which instance will be A's component.
For instance :
class A{
private:
//Some attributes
Comparator<A> comp_;
public:
//Some methods (including constructor)
bool operator()(const MyObject & first, const MoObject & second) const
{
return comp_.compare(first,second);
}
void setComparator(Comparator<A>& comp){
comp_ = comp;
}
}
//Forgot the syntax about template. So there is a template with one parameter
class Comparator{
public:
//Constructor
//To overwrite on subclasses
virtual bool compare(T& first, T& second) = 0;
}
With that configuration, you can easily add a method, without modifying A, just set the right comparator at any moment of program's execution.

accessing a pointer member variable in derived class [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have following code:
class B{
protected:
X *x;
public:
function(char *data){
// access x
// works fine
}
};
class D: public B {
function2(char *data)
{
// access x
// gets garbage
}
};
I have a member variable which is pointer. moreover this pointer is inside "data" but when i access in class D it shows garbage.
can some body please help me...
I've put on my psychic debugging hat, and come to the conclusion you're probably doing something like this:
class Packet
{
public:
Gizmo* gizmo_;
};
class Processor
{
public:
void ProcessPacket(char* packet);
};
// ...
Packet packet;
// packet filled with goodies
Processor proc;
proc.ProcessPacket(reinterpret_cast<char*>(&packet.gizmo_));
Am I right?
If so, my guess is that the Gizmo you're pointing to has somehow fallen out of scope -- thereby becoming destroyed -- or you performed the cast incorrectly.

C++ method calls from the same class [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
In C++, I want to be able to call a method in the same class without creating an object of the whole class. The class is huge and I do not want to create a huge memory location for an object. I am used to programming in C#.
In C# I could do this
class test()
{
private void A()
{
B();
}
private void B()
{
doSomething;
}
}
in C++ I am under the impression I have to do.
class test()
{
public:
static void A();
void B();
};
void test::A()
{
test t;
t.B();
}
void test::B()
{
doSomething;
}
}
I do not want to make B() static nor do I want to create and object of test because in reality my class is a lot larger than this, and creating a object of the class would use memory that I do not want to.
Is there a way I can accomplish what I could in C# in C++?
No. If B needs an object, you have to give it an object. If B doesn't need an object, declare it static. C# is no different -- in your example, A is not static so the object already exists.
static void A();
void B();
You cannot use static function to call non-static one at all.
Solution:
Mark B as static too (if it doesn't depend on current object) and thus you don't have to creat a new object. Else I think A should be non-static.

how to over come the issue of object slicing in c++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
How should I get rid of the problem with object slicing in c++.
In my application if the derived class has some dynamically allocated pointer and derived class object is assigned to base class object, the behavior is memory corruption!
It depends on your design. You may have to change certain design criteria to get rid of it. One of the options is to have an overloaded operator = and copy constructor in your base class for particular derived class.
class Derived;
class Base
{
//...
private:
Base (const Derived&);
Base& operator = (const Derived&); // private and unimplemented
};
Now if you attempt to do something like following:
Derived d;
Base b;
b = d; // compiler error
it will result in compiler error.
you can't. you should solve the problem with the pointer. if you want to assign Obj2 to Obj1, override assign operator (operator=)