0

I want to create a vector of objects, most likely derived from a base class, which might or might not have their own personal non-derived functions. As of yet I am unable to call these functions as they are not part of the class used in the declaration of the vector.

How would one go about on making this possible?

Code below gives the error:

../src/ObjectVectors.cpp:22:33: error: no member named 'getInt' in 'BaseObject'

//============================================================================
// Name        : ObjectVectors.cpp
// Author      : Edwin Rietmeijer
// Version     :
// Copyright   : This code is owned by Edwin Rietmeijer as of 2014
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>
#include "BaseObject.h"
#include "SubObjA.h"


using namespace std;

int main() {
    vector<BaseObject *> objectVector;
        objectVector.push_back( new SubObjA );



    cout << objectVector.front() -> getInt() << endl;

//  for ( pos = objectVector.begin(); pos != objectVector.end(); ++pos )
//      cout << pos -> get()  << endl;
}

/*
 * BaseObject.h
 *
 *  Created on: Feb 11, 2014
 *      Author: edwinrietmeijer
 */

#ifndef BASEOBJECT_H_
#define BASEOBJECT_H_

class BaseObject {
public:
    BaseObject();
    int get();
    virtual ~BaseObject();
};

#endif /* BASEOBJECT_H_ */

/*
 * BaseObject.cpp
 *
 *  Created on: Feb 11, 2014
 *      Author: edwinrietmeijer
 */

#include "BaseObject.h"

BaseObject::BaseObject() {
    // TODO Auto-generated constructor stub

}
int BaseObject::get(){
return 0;
}

BaseObject::~BaseObject() {
    // TODO Auto-generated destructor stub
}

/*
 * SubObjA.h
 *
 *  Created on: Feb 11, 2014
 *      Author: edwinrietmeijer
 */

#ifndef SUBOBJA_H_
#define SUBOBJA_H_

#include "BaseObject.h"

class SubObjA : public BaseObject {
    int data = 88;
public:
    SubObjA();
    int getInt();
    virtual ~SubObjA();
};

#endif /* SUBOBJA_H_ */

/*
 * SubObjA.cpp
 *
 *  Created on: Feb 11, 2014
 *      Author: edwinrietmeijer
 */

#include "SubObjA.h"

SubObjA::SubObjA() {
    // TODO Auto-generated constructor stub

}

int SubObjA::getInt() {
    return data;
}

SubObjA::~SubObjA() {
    // TODO Auto-generated destructor stub
}
staxas
  • 149
  • 12

3 Answers3

2

You can downcast with dynamic_cast. It returns null if its not a valid cast.

Sanjaya R
  • 6,246
  • 2
  • 17
  • 19
  • 1
    Correct, however it is also worth pointing out that if your code looks like if(dynamic_cast(ptr)) else if (dynamic_cast(ptr)) then it is generally accepted that this is an indication of a design flaw in your program. – blockchaindev Feb 11 '14 at 16:01
2

I would use 'visitor' pattern. It will give you similar possibility as dynamic_cast, however, I found 'dynamic_cast' strongly discouraged in many books/articles.

Bogdan
  • 984
  • 8
  • 16
1

I'd recommend using polymorphism.

declare int get() in BaseObject as virtual:

virtual int get() { return 0, }

overload it in subObjA:

virtual int get() { return data; }  // the virtual isn't mandatory here, just easier to read

int get() override { return data; }  // c++11 version

this way, when you call BaseObject->get, it will call SubObjA->get()

google polymorphism.

or you could dynamic_cast<SubObjA>, but I don't think it's the best approach here...

Syl
  • 2,733
  • 2
  • 17
  • 20
  • I understand that overloading the get() function would be the desired approach, but I am using a differently named function on purpose for testing purposes. I want to create an array of objects from which the functionality differs so much, that they each have their own specifically [named] functions. Is there a solution to that, or would I have to approach the 'vector of pointers' solution a little differently? – staxas Feb 11 '14 at 15:48
  • 1
    That's called _overriding_. – Lightness Races in Orbit Feb 11 '14 at 15:48
  • my bad (about overriding). Yes, you can`dynamic_cast(objectVector.front())->getInt()`. – Syl Feb 11 '14 at 15:53
  • But you would need to know the object derived classs up front of course. Is there any way to create a 'dumb' vector of pointers? I want the objects to identify themselves via a base class function ( say, 'identify()') at which point the program knows what functions are available. The objects would be different types of generators with their own specific functionality. I would only use pointers to find out where they are in memory, and a vector seems the way to go to order the process of adding new generators. – staxas Feb 11 '14 at 15:58
  • 1
    Bogdan posted a good solution, visitor pattern. – Syl Feb 11 '14 at 16:19