C/C++ :: Access Derived Classes Functions On A Vector
Jan 19, 2014
I need to access the functions of the derived classes from a vector of objects of base classes (can't believe I wrote it). Here a Diagram for you to understand:
So as you see, I need the function Use() from the Usable class, to be able to be called from the vector like:
inventory.at(x)->Use()
View 14 Replies
ADVERTISEMENT
Jul 11, 2013
I have a big problem with searching a solution for getting access on getters and setters of the derived classes of an interface.
Interface:
class IfParam
{
public:
IfParam();
};
Now I have a derived class for each needed datatype. E.g.
class Int32Param: public IfParam
{
public:
Int32Param();
Int32Param(IfParam* l_param);
int getValue();
void setValue(int l_value);
private:
[Code]...
My Problem now ist getting access to the getters/setters over the interface. I wish I could initialize the Params like this:
IfParam* param = new Int32Param();
param.setValue(12);
IfParam* param = new StringParam();
param.setValue("String");
But to have access to the getter/setter I have to declaire the functions in the interface as generic functions. But how? I tried to use temlates, but then i have to declaire IfParam as IfParam<int>. Thats a problem because in my original program I do not know which TypeParam the IfParam interface will be initialized with when I create the pointer.
View 7 Replies
View Related
Jan 20, 2013
I have the following code which will find the minimum size queue among a vector of queues and the minimimum size queue will enqueue(push) the int
std::vector<std::queue<int> > q
void enqueue(){
int min_index = 1;
std::size_t size = q.size();
for( i=2; i<size; i++) //accessing loop of queues
if(q[min_index].size() > q[i].size())
min_index = i; // Now q[min_index] is the shortest queue
q[min_index].push(int)
}
Now my another paradigm is to do the dequeue(pop) operation in another function(shown below), bt i need to access all vector of queues declared in enqueue() function. how can i access the loop of queues given in the enqueue() function?
void dequeue(){
//q.pop operation , access all the queues in the loop of queues
}
willq[i].pop(int);
Access all the queues in the enqueue function and does the pop operation?
View 1 Replies
View Related
May 5, 2013
I've created a base DLL for all my future DLL's, a way of getting version numbers and such and that compiles fine, but I can't add it into a class for a new DLL. All the headers do have an appropriate cpp to define the function declarations (and they compile fine).
All for the base DLL I have:
LibVer.h
Version.cpp
Function.cpp
LibVer.h
#pragma once
#include <vector>
#define DLLEXPORT 1
#define DLLIMPORT 2
#define DLL DLLIMPORT
[Code] .....
View 6 Replies
View Related
Apr 7, 2014
As long as no base class constructor takes any arguments, the derived class need not have any constructor, if one or more arguments are used then it is mandatory for the derived class to have a constructor and pass the arguments to base class constructors. While applying inheritance, we usually create objects using derived class. Then it makes sense for the derived class to pass arguments to the base class constructor. When both the base and derived class contain constructors ,the base class constructor is execute first.
In case of multiple inheritance, the base classes are constructed ,in the order in which they appear in the declaration of the derived class. Similarly in a multiple inheritance the constructors will be executed in order of inheritance. Since the derived class takes the responsibility to supply initial values to the base class,we supply the initial values that are required by all the classes together where the derived class object is declared.
The constructor of the derived class receives the entire list of values of arguments and pass them on to the base constructors int the order in which they are declared in the derived class
View 1 Replies
View Related
Apr 6, 2013
Is it possible to overload a variable in a derived class? Example:
struct Circle
{
int radius() const { return r; }
private:
int r;
}
struct Smiley : Circle
{
// inherits the function int radius() const, but doesn't return Smiley::r
private:
int r;
}
View 7 Replies
View Related
Jul 29, 2014
Code:
class Var {
public:
Var();
~Var();
private:
QMap<QString, QSharedPointer<Data>> _mapVars;
};
QDataStream &operator<<(QDataStream &stream, const QSharedPointer<Data> p_data);
QDataStream &operator>>(QDataStream &stream, Data &p_data)
I want to serialize _mapVars into a file. However, I have many other classes that are derived from Data, do i need to check for Data type inside the overloaded << functions like below in order to serialize ??? This doesn';t seem to be very correct ...
Code:
QDataStream &operator<<(QDataStream &stream, const QSharedPointer<Data> p_data) {
if(p_data->GetType == .....) {
}
}
View 7 Replies
View Related
Apr 16, 2014
S I want to have different >> operators for several derived classes. Has I tested...
Code: class base{
friend std::istream & operator>>(std::istream & in, base & v);
public:
base();
[Code]......
I noticed that the base operator is the only one being called for all 3 objects. which makes sense and now that I think about it I am more surprised that the "derived operators" compiled at all.
Still, how would I go about declaring different overloaded operators for the different classes?
View 4 Replies
View Related
Mar 11, 2013
class A {
// is abstract
};
class B : public A {
// ...
};
[Code] ....
[Code] ....
main3.cpp: In member function ‘FooB& FooB::operator=(const FooC&)’:
main3.cpp:46:44: error: expected ‘(’ before ‘other’
main3.cpp:46:49: error: no matching function for call to ‘Foo<C>::Foo(const FooC&)’
main3.cpp:46:49: note: candidates are:
main3.cpp:19:2: note: Foo<T>::Foo() [with T = C]
main3.cpp:19:2: note: candidate expects 0 arguments, 1 provided
main3.cpp:16:25: note: Foo<C>::Foo(const Foo<C>&)
main3.cpp:16:25: note: no known conversion for argument 1 from ‘const FooC’ to ‘const Foo<C>&’
Is there any way to make it work?
View 1 Replies
View Related
Apr 30, 2013
how to create a base class that is derived (inherited) by three other classes?
View 12 Replies
View Related
Nov 21, 2013
I use a CArray<CClase1,CClase1> m_Clases1.
CClase1 is derived of CObject. " class CClase1 : public CObject"
When, at last I do : "m_Clases1.RemoveAll()" , I suppose that the CClase1 destructor is called. But when i do this the program fails.
View 6 Replies
View Related
Oct 1, 2013
I would like to access (use) a (virtual) method declared in a base class via a pointer to an object belonging to a derived class, which overrides the base method. Is it possible? Up to now I have not been successful. I am including a program with a more detailed description.
// declare a base class T0 with virtual function P, and a derived
// class T1 which overrides P; declare two ptrs, to T0 and T1,
// and generate corresponding objects; calling P via the two
// ptrs gives expected result; however I would like to take
// ptr to T1 object & execute the base function (declared
// in T0); is it possible? I tried all possible casts but nothing
// works; no matter how I cast the pt1 ptr, I always end up executing
// the overriding function (declared in T1)
// actual output (debian 7, gcc 4.7.2-5):
[code]....
View 5 Replies
View Related
Mar 20, 2014
I'm a little confused by my programming assignment this week. I've been working at it Wednesday and I've made progress but I'm still confused as to how I'm supposed to do this. The class I made is called Stack, and it's derived from a template class called StackADT. We also utilize a class called unorderedLinkedList, which is derived from a class called linkedList.
We're supposed to implement all of the virtual functions from stackADT in the Stack class. The Stack data is stored in a an unorderedLinkedList, so what I'm confused by is how to implement a few of the Stack functions because there are no functions in unorderedLinkedList which we could call to manipulate the data.
As you can see from my attached code, I'm really confused by how I'm supposed to implement the pop() and top() functions, and I also think my initializeList() function is wrong. We don't have any similar functions in unorderedLinkedList to call, so I'm at a loss of how i'd access my unorderedLinkedList. My initial thought was to call the similar functions in the class that unorderedLinkedList was derived from, linkedList, but I'm unsure of this is what we're supposed to do, or if theres actually a way to access my unorderedLinkedList without having to use the functions from the base class.
NOTE: We're not allowed to modify stackADT, unorderedLinkedList, and linkedList.
Stack.h
#include "stackADT.h"
#include "unorderedLinkedList.h"
template<class Type>
class Stack: public stackADT<Type>{
template <class T>
struct nodeType
{
T info;
nodeType<T> *link;
[Code]...
View 3 Replies
View Related
Mar 6, 2015
I have a hpp file with a list of inline finctions like this:
Code:
inline int check() {
return 1;
}
inline int check_1() {
return 1;
}
... What I would like to do is to include them into several unrelated classes. How can I do this. Can I just add the hpp inline functions in headers of my class containing files or not. I mean if they are not defined as class functions how can they be called. I don't understan the logic.
View 2 Replies
View Related
Oct 19, 2014
I recall when I first started playing with C++ I was told that you should never use virtual functions unless you absolutely cannot think of a better way to do whatever you are attempting. This is something I have tried to stick to over the years - and indeed is probably why I have never used inheritance or polymorphism much in my own programmes.
However, I notice through a great deal of the code examples offered to questions here and even over on StackOverflow that commentators show no hesitation to recommend code that involves virtual functions. More so, I have even seen several instances here where - what I was taught as, but they may well have a different official name - 'pure virtual functions' (those with definitions inside a class of something like virtual int function_name(void)=0) are demonstrated and I was very clearly taught to avoid those like the plague.
I was wondering therefore has the official thinking changed since the middle nineties on when - and even whether - to use virtual functions in your programmes?
View 9 Replies
View Related
Jan 26, 2013
when I should use pure virtual functions.On the one hand, "TOY" for example should be an abstract class since theres no such thing as "TOY" , there are "toy cars", "toy fighters" etc , but on the other hand I need to force it somehow to be abstract since theres no really a function that any toy should have and implement on his own way (except PRINT maybe).
when I should REALLY use pure virtual functions? And if I want to avoid people from creating TOY objects (for example), the only way is PURE virtual functions. right?
View 4 Replies
View Related
Sep 11, 2014
So I have 2 seperate base classes, (note that I removed the variables and functions that do not relate to the topic) Object.h
class Object{
public:
Object();
~Object();
[Code].....
The error I get is saying I am calling a function declared with one calling convention with a function pointer declared with a different calling convention and this makes perfect sense because for some reason, the function pointer is pointed at the virtual function Object::update but I can't figure out why and how to make it point at the virtual function Drawable::getImage.
Also, the virtual update function is called in a different place just before this and works correctly.
View 6 Replies
View Related
Dec 29, 2013
I have a situation: I have a class character
class Character;
class Village;
class Character {
public:
//Functions
void charGen(); //Character creation
[Code] .....
According to the Friendship and Inheritance tutorial [URL] ...., that code should work, but it doesn't. I am given an error: undefined reference to questsCompleted
The compiler I am using is Code::Blocks ....
View 4 Replies
View Related
Jul 5, 2013
I'm developing a stack using a linked list. I have to develop two friend functions to do something with the list. But I've tried to understand the friendship, without any consistent result.
Extract of code:
Code:
class Element {
public:
int Data;
Element *nextElement;
Element(int Element) {Data = Element; nextElement = NULL;}
~Element() { }
[Code] ....
I have no errors, but I cannot access the private properties of the class.
The thing I would like to do is: sum the elements of the stack with a friend function.
View 7 Replies
View Related
Jan 10, 2015
I can not seem to add a vector to my head file. I have tried many things and can't figure it out.
Compile and Compile error -
g++ -Wextra -pedantic -std=c++11 Card.cpp Card.h Deck.h Deck.cpp unit_test1.cpp ;
Deck.h:15:27: error: invalid declarator before ‘deck’
void std::vector<Card *> deck;
[Code]....
View 2 Replies
View Related
Mar 30, 2013
Say I have 3 classes:
class Player {
public:
virtual func1();
[code]....
Say in my main class, I have a function fight(Player p1, Player p2) and I would like to do something like this in the fight function, given that p1 is the human and p2 is the computer:
//function fight()
fight(Player p1, Player p2) {
p1.func2();
}
//using function fight()
fight(human, computer);
When I compile the program, I got this: error: ‘class Player’ has no member named 'func2()' What can I do to allow p1 to call func2 inside fight()? I'm not allowed to use pointers as the parameter for fight() and have to use the signature fight(Player p1, Player p2).
View 6 Replies
View Related
Jan 21, 2014
The compiler creates virtual table for the base class and also for the derived class whether we override it or not.
That means each class has separate virtual table. when we get the size of the each class with out any data members... the size of base is -- 4 bytes(64 bit) and the size of derived is -- 1
The size of base class 4 is correct since it creates the virtual pointer internally and its size is member data + virtual pointer, but it in this case I have included any data members so it has given 4 byts.
But why in case of derived is 1 byte, since it the derived class has overridden the virtual function from base, this will also contains the virtual pointer which will be pointing to derived class Vtable, it the size of the class suppose to be 4 instead of 1 byte.
#include<iostream>
class A{
public:
[Code].....
View 1 Replies
View Related
Sep 16, 2013
Question: How can we write functions to access various fields?
Example: A phone number -- it has an area code, exchange & subscriber -> (123)-456-7890
How can we write a function to access the various fields of a phone number both in a particular class & in the main?
View 5 Replies
View Related
Dec 6, 2014
I am working on a school project and am stuck in the debugging. I am compiling with g++ and am using c++11.
Error message
In file included from /usr/include/c++/4.7/algorithm:63:0,
from date.h:7,
from date.cpp:1,
from schedule.h:1,
from schedule.cpp:1,
from date_driver.cpp:1:
[Code] ....
Here are my overloaded operators for my date class.
bool Date :: operator == (Date otherDate){
if (((otherDate.getDay () == getDay ()) && (otherDate.getMonth () == getMonth ())) && (otherDate.getYear () == getYear ()))
[Code] .....
View 1 Replies
View Related
Feb 28, 2014
Suppose I make a class, something like having the constructor being invoked first makes sense, I don't have a problem with that. But, how could I limit access to functions until certain functions are called? Perhaps this isn't built into the language so you can't. And maybe this problem never comes up. For example if you have a set() and get() functions, if they are both public functions, there doesn't seem to be a way for the compiler at least now if set() never gets called you shouldn't call get(). I just see this as error prone if you need to use libraries, you have to know not to do it from documentation instead of something the compiler can check.
View 11 Replies
View Related
Oct 14, 2014
How can I access a function in a child form from another child form of a same owner?
View 1 Replies
View Related