if (object of a specific class)

Mar 27, 2011 at 2:15pm
Hey guys,

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):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

class A
{
}

class B
{
}

class C
{
}

class D
{
public:
   void access();
}

void D::access()
{
   if (object is from class A)
       cout << A;
   else if (object is from class B)
       cout << B;
   else if (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!
Mar 27, 2011 at 2:20pm
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.

See here: http://www.cplusplus.com/doc/tutorial/inheritance/#inheritance

Is there any specific trouble friend?

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/
Last edited on Mar 27, 2011 at 2:23pm
Mar 27, 2011 at 2:44pm
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.

Thank you for your help! :)
Mar 27, 2011 at 4:56pm
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.


This is exactly what inheritance and virtual functions are for:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class D
{
public:
  virtual void access() = 0;
};

class A : public D
{
public:
  virtual void access() { cout << "A"; }
};

class B : public D
{
public:
  virtual void access() { cout << "B"; }
};

class C : public D
{
public:
  virtual void 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.
Last edited on Mar 27, 2011 at 4:57pm
Mar 28, 2011 at 8:04am
Ah right I understand now, sorry for that guys and many thanks for the help
Topic archived. No new replies allowed.