Implement several order relations for one class? [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 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.

Related

Using all functionality of OOP in three different classes [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 9 years ago.
I have a homework to do, I need to create three classes Student, Profesor what inherits from a class list of faculty and I dont know how to chain all OOP functionality (and must), I need to use templates and not a builtin list I need to alocate dynamic list of objects of students and profesors order by departament, I need to use static virtual methods namespace and operators rewrite, why I will need usage of virtual metods there? And usage of namespace?
I started this, but I'm in trouble, how to get a good structure to use all of the OOP functionality?
Thanks for tips!
Skiny header sourcecode:
ifndef FACULTATE_H
#define FACULTATE_H
class Facultate
{
char *nume;
list<Profesor*> profesori;
list<Student*> studenti;
public:
void addProfesor();
void addStudent();
Facultate();
virtual ~Facultate();
Facultate(const Facultate& other);
Facultate& operator=(const Facultate& other);
ostream& operator<<(ostream& O, const Facultate &F);
protected:
private:
};
#endif // FACULTATE_H
There are many ways to organize this.
Here is one layout:
class Person;
class Student : public Person;
class Facultate : public Person;
class Teacher : public Facultate;
class Staff : public Facultate;
The Person class would contain attributes common to Students and Teachers, such as first and last name.

C++ Generic use of parent class (reference or pointer) [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 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.

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);
}

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.

Is it possible to edit template name at compile time? [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 am trying to modify template type given at compile time but unable to do it. Let see if you have some idea.
Lets consider we have 2 classes class A and A_test and one template class B.
Will implemented class B as it is having a function b_f() which is internaly creating an object of template class and calling public funcion A_f()/A_test_f() of class A or Class A_test.
from main, will be creating class B obj; obj.b_f();
but I want to creat an object of class A_test not class A.
Let me know if it is possible.
Basicaly I wanted to do object injection. Please let me know if it is possible.
The best solution by far is to try to rename A_test_f() to be the same as A_f().
If that proves impossible, the next thing I'd try is to specialize class B:
template<class AT>
class B {
public: b_f() {
AT m_A;
m_A.A_f();
};
template<>
class B<A_test> {
public: b_f() {
AT m_A;
m_A.A_test_f();
};
If B is too complicated, there are other things to try, but you probably want to rethink what you're doing first. If all else fails, do what I coded above here, but call it B_HELPER instead of B and then b_f() can do: B_HELPER<AT> m_A; m_A.b_f(); That way you don't have to recode all of B.
The common way to inject dependencies into templates at compile-time is through type traits. This allows customizing the templates externally through another struct or class which has specific knowledge of the class. Examples in the standard library include std::char_traits<> and std::iterator_traits<>. Boost also defines some, including boost::type_traits<>.
Traits involve defining a struct for the general case and specializing it for alternate cases when necessary.
// general case: select method named "f".
template<class T> struct b_traits
{
typedef void(T*F)();
static const F f = &T::f;
};
// template type that forwards method selection to "b_traits" struct.
template<class AT>
class B {
public: b_f() {
AT m_A;
(m_A.*(b_traits<AT>::f))();
};
class A_test { ... };
// special case: select method named "A_test_f".
template<> struct b_traits<A_test>
{
typedef void(T*F)();
static const F f = &A_test::A_test_f;
};
int main ()
{
B<A_test> b;
b.b_f(); // will invoke "A_test::A_test_f()" rather than "A_test::f()".
}