LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-17-2010, 05:50 AM   #1
disruptive
Member
 
Registered: Dec 2005
Posts: 76

Rep: Reputation: 15
C++ class issue and identity


Hi there

I am attempting to modify some code for my own purposes but am finding things tricky as the code is not so well documented with a lot a custom types. As I put things below you should hopefully build a better picture of what I am trying to do.

I have what looks like the following set up.

1. Create a existence of a class based on what appears in a text file. Call this class, class SpecialistType. There are a number of classes that can be instantiated here. Now class SpecialistType inherits class BType. i.e. we have the following sort of definition:

Code:
 class SpecialistType : public BType {
2. We also have a B class which is instantiated later on in the program. One of the variables passed to it on start-up what looks like a pointer of BType.

3. Later on the methods in B class are executed.

For example:

Code:
class B {
  public:
    B(.., .., ..., BType* bt)
Now, because each class does something but is related, I want to put some logic in the code that decides what to do depending on the earlier instantized SpecialistType class.

However I now want to figure out from bt, which type of SpecialistType we have already instantiated. I just don't know if there is anyway to do this, given that we cannot read from the text file anymore.

No I could prob do this by putting some extra fields in some of these classes - but I'd like to make minimum changes to the code. This might seem a silly question, but is there any way I can get a handle on what class was instantiated? Or is it an extra field in BType?

Thanks in advance.
 
Old 08-17-2010, 08:54 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Maybe RTTI can help you.

http://en.wikipedia.org/wiki/Run-time_type_information


P.S. Here's an example using typeid()
Code:
#include <iostream>
#include <typeinfo>

class Base
{
public:
   Base() {}
   virtual ~Base() {}
};

class DerivedA : public Base
{
public:
   DerivedA() {}
   virtual ~DerivedA() {}
};

class DerivedB : public Base
{
public:
   DerivedB() {}
   virtual ~DerivedB() {}
};


void whatIsIt(const Base* base)
{
   if (typeid(*base) == typeid(Base))
   {
      std::cout << "Base object." << std::endl;
   }
   else if (typeid(*base) == typeid(DerivedA))
   {
      std::cout << "DerivedA object." << std::endl;
   }
   else if (typeid(*base) == typeid(DerivedB))
   {
      std::cout << "DerivedB object." << std::endl;
   }
   else
   {
      std::cout << "Unknown class object." << std::endl;
   }
}

int main()
{
   Base* b  = new Base;
   Base* ba = new DerivedA;
   Base* bb = new DerivedB;

   whatIsIt(b);
   whatIsIt(ba);
   whatIsIt(bb);
}

Last edited by dwhitney67; 08-17-2010 at 09:11 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ class issue disruptive Programming 1 08-16-2010 10:47 AM
Samba logging issue shows error in smbd.log : unrecognised vfs class ksharma Linux - Software 0 02-18-2010 03:44 PM
C++ templated Node class: pointers to different instantated class types jhwilliams Programming 3 08-20-2007 06:20 PM
c++ class issue k1ll3r_x Programming 4 09-25-2006 11:04 PM
Which C++ editor in Linux has the class view/class browser feature imaginationworks Programming 7 05-21-2006 11:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration