like the Object class C++

Hello all,

i would like to create a method that receives an object as a datatype and can be checked in this method if it's a sting or a vector<string> for using in different actions inside my method.

this can be done in Java using the Object Class, can i do something like that in c++


thanks in advance
If you mean Object.getClass(), there isn't really an equivalent for all types in C++; and specifically, not for string and vector<string>.

For classes that have virtual functions, there is RTTI (runtime type identification). In Java, all methods are virtual. This is not the case in for C++ classes.

You may find this relevant. http://www.cplusplus.com/forum/general/21246/
Last edited on
There are ways to do this (see the typeinfo header and typeid()), but they're ill advised. I don't recommend you use them for this.

EDIT: the thread kbw linked to shows how to use typeinfo. But again I recommend you avoid doing that as it's a lot easier to screw up and generally has worse performance since the type needs to be determined at runtime rather than compile time.

Generally, in C++ you don't need to check for this yourself at runtime. Instead, you let the compiler do it at compile time. This can be accomlished by overloading the function to take different types.

For example, say you want to have a function called 'Bar' which does something different depending on whether or not the passed type is a string or vector<string>:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Bar(const string& a)
{
  // here, 'a' is a string.
}

void Bar(const vector<string>& a)
{
  // here, 'a' is a vector of strings
}

// then you can just call Bar() with whatever type and the appropriate function
//  will be called
int main()
{
  string a;
  vector<string> b;

  Bar(a);  // calls the string Bar
  Bar(b);  // calls the vector<string> Bar

  return 0;
}




If you want to take that a step further, you can add in templates. For example... let's say you have a bunch of code in Bar that you want to be the same no matter what type you're dealing with... and only a few lines of code that will be different if you have a string/vector:

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
// Have your 'Bar' functions which do tasks that are specific to
//  strings / vectors / etc
void Bar(const string& a)
{ // ...
}

void Bar(const vector<string>& a)
{  // ...
}

// Have a template 'Foo' function which does a bunch of stuff the same
//  regardless of the type:

template <typename T>
void Foo(const T& a)
{
  // this function called for all types.
  DoWhateverIsCommonToAllTypesHere();

  // then when you need to do something specific to a single type
  //  you can call the appropriate 'Bar' function
  Bar(a);   // will call the appropriate one automatically
}

//  Then in main() you can just call Foo normally

int main()
{
  string a;
  vector<string> b;

  Foo(a);  // etc
  Foo(b);

  return 0;
}
Last edited on
Thank u Disch for ur valuable reply. It helped me much to solve the problem.

Thank u
+1 what Disch said! :-D

I add another one: Sometimes, you want to do something for a whole category of classes, e.g. you want some functionality to work for all vector's - be it vector<int> or vector<string> or whatever. You can use templates again:

1
2
3
4
5
6
7
8
template<class T>
void Bar(const vector<T>& a)
{
    // this will be called if you call it with any vector.
    // except if you have some more specific version. E.g. if you also have 
    // the example from Disch with "void Bar(const vector<string>&)" then the Disch-Bar is called
    // instead of this one here for vector<string>. Cool, eh?
}


One more hint about this technique: You should avoid to overload similar integral types, especially those which can be converted to each other. Having an own "void Bar(float)" and another "void Bar(int)" could give you some unwanted calls to the wrong function, if you are not carefull..

Ciao, Imi.
Topic archived. No new replies allowed.