Just a quick question for you all. I want to make a function that has ifs and else ifs, However, I would like the condition to be (object of a specific class). Say for example (ignore the lack of information, i just want to demonstrate my idea):
class A
{
}
class B
{
}
class C
{
}
class D
{
public:
void access();
}
void D::access()
{
if (object is from class A)
cout << A;
elseif (object is from class B)
cout << B;
elseif (object is from class C)
cout << C;
}
void main()
{
A a;
a.access();
B b;
b.access();
C c;
c.access();
}
Ideally, I don't want to have to put the function "access" in all the classes unless I really have to.
Hope you guys get what I mean and can help, thanks for the help!
This sounds like inheritance, this is what C++ is famous\infamous for. What's the trouble? You want to establish a base virtual class and have all of the other classes inherit from it.
EDIT: A template might also help. This might be a little more flexable depending on if you want to use it outside of your classes for instance: http://www.cplusplus.com/doc/tutorial/templates/
Hmm... kind of... i've read inheritance and in my real code it is implemented, but the issue that I face is, I want to know of a command where I can get my function access to do a different thing according to the different class object that is called.
I know that simply I could function overload in order to rewrite the access function in each of the derived classes but I was wondering if I can just have it like I explained above.
I will read about templates now and see if this solves my problems.
class D
{
public:
virtualvoid access() = 0;
};
class A : public D
{
public:
virtualvoid access() { cout << "A"; }
};
class B : public D
{
public:
virtualvoid access() { cout << "B"; }
};
class C : public D
{
public:
virtualvoid access() { cout << "C"; }
};
void SomeFunction(D& d)
{
d.access(); // will print A, B or C depending on what type of class 'd' really is
}
int main()
{
A a;
SomeFunction(a); // prints A
B b;
SomeFunction(b); // prints B
// etc
}
I was wondering if I can just have it like I explained above.
You don't want it that way. If chains are horrible for this kind of thing because they're a maintanance nightmare and they break encapsulation.
With your approach, if you change A in the future, you have to remember to also change D::access (and any other function that does a similar thing). Same thing if you add a new class that you want to have an access function. Failure to modify D::access appropriately would screw up your program.
With the approach I showed above, the only thing you have to change is the class you want to change. If you want to change A, all you have to change is A. No need to worry about any other functions. If you want to add a new class, just add a new class and it will automatically work with all existing code without you having to modify anything about it.
Generally if you're writing code that checks to see what kind of object something is.. you're probably doing something wrong.