C++ :: Class Pointer And Virtual Function Couldn't Be Avoided?

Mar 16, 2013

Let's look at this simplified code, it gives compilation error

#include <iostream>
using namespace std;
class A {
public:
void showInfo() { cout << " This is an instance of A" << endl; }

[Code] ....

Without using pointer, how to make this works?

View 8 Replies


ADVERTISEMENT

C/C++ :: Use Virtual Function In Class In Which Virtual Function Is Defined?

Dec 27, 2012

class Parent{
  public:
virtual int width();
    virtual int height();
    int area(){return width()*height();};

[Code] ....

View 10 Replies View Related

C++ :: Virtual Table Pointer - Size Of A Class?

Apr 22, 2015

Here is an example,

Code:
class A {
public:
virtual void foo(){}
virtual void foo2(){}
virtual void foo3(){}
};
int main() {
A a;
int ret = sizeof(A);
return 0;
}

Basically object a contains a virtual table pointer which is of size 4 bytes. Since class A should have a virtual table which contains three pointers pointing to foo, foo2,foo3 separately. So the virtual table should be of size 12 bytes. I wonder where is virtual table located in memory?

View 2 Replies View Related

C++ :: Virtual Function Table Pointer Misplaced In Object Memory

Jul 29, 2014

I have found that when I dump a C++ object from memory to a file - it seems that there is a misplacement of the last Virtual-Function-Table pointer - in that appears at the beginning. The result is that the gdump information based on this object dump (using green hills) is incorrect. I copied the contents of the gdump information below. The executable is compiled in linux.

Basically MEIO::CameraStatus contains an item that relates to its parent class (line 188). Then it has 18 items that are all Diagnostics::EventsCounter items. Now for each Diagnostics::EventsCounter item there is a Virtual-Function-Table Info Pointer as its last item. All is fine and good except that the last item of MEIO::CameraStatus which is _selfReset is missing its last item of 4 bytes (which is the Virtual-Function-Table Info Pointer ). On the other hand - right before the first Diagnostics::EventsCounter item ("_vidErrors") - there is an extra 4 bytes which happens to be the Virtual-Function-Table Info Pointer. As I said the gdump information file does not see this.

Why the object memory "moves" the last Virtual-Function-Table Info Pointer to the beginning (right before _vidErrors) and is there a way to "fix" this?

///////////////////////////////////////////////////////////////////////////
"MEIO::CameraStatus" val:0x000002f0 ind208,-1) Struct-Begin Info
188: "" offset 0, Parent-Class Private Info C++ Struct ref = 114
189: "_vidErrors" offset 160, Member Info C++ Struct ref = 128
190: "_vdiErrors" offset 480, Member Info C++ Struct ref = 128

[Code] .....

View 4 Replies View Related

C++ :: Abstract Class And Virtual Function?

Feb 17, 2013

I have this header file called Shape.h containing these function declarations. and a Shape.cpp which contains the body of the function. I am not showing it since it is not needed.

//This is from Shapes.h header file
#ifndef SHAPES_H
#define SHAPES_H
#include <iostream>

[Code]....

I have this unfinished Main.cpp because the third line "JuanSanchez::Circle *pCar = new Circle; " is giving me a compiler error "error C2061: syntax error : identifier 'Circle' "

#include "Shapes.h"
int main()
{
const int arrayIndex = 4;
JuanSanchez::Shape *myShape[arrayIndex];
JuanSanchez::Circle *pCar = new Circle;
}

What Could be causing this error?

View 8 Replies View Related

C++ :: Calling Defined Function Pointer From Another Pointer To Class Object?

Aug 19, 2014

I am attempting to implement function pointers and I am having a bit of a problem.

See the code example below; what I want to be able to do is call a function pointer from another pointer.

I'll admit that I may not be explaining this 100% correct but I am trying to implement the code inside the main function below.

class MainObject;
class SecondaryObject;
class SecondaryObject {
public:

[Code]....

View 10 Replies View Related

C++ :: Size Of Derived Class With Overriding Virtual Functions From Base Class?

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

C++ :: Pointer-to-member Array Crashes With Virtual Inheritance?

Sep 30, 2014

I've got the following code which demonstrates a problem :

Code:
struct A {
double x[2];
double i;
};
struct B : virtual public A {
double y[2];

[code]....

I'm wondering if this is a compiler bug. Why doesn't the pointer-to-(derived)-member work for the array if it works for the non-array?

View 4 Replies View Related

C++ :: Function Pointer To Non-static Class Member

Aug 19, 2014

I have the following problem: I am using NLOpt for optimization. The API provides functions to set the objective. This is done as follows:

double objective(const vector<double> &x, vector<double> &grad, void *data)
{
return x[1]*x[0];
}
int main(){
nlopt::opt opti(nlopt::LD_MMA,2);
opti.set_min_objective(objective,NULL);
vector<double> x(2);

[Code]....

Now I want to make the function objective a member of a class:

class Foo {
public:
double objective(...){..}
};

How can I give this method to opti.optimize? If I make objective static I can use

opti.optimize(Foo::objective,NULL);

but I do not want to have a static member. Is it possible to create an object of type Foo and give it to opti.optimize?

View 1 Replies View Related

C++ :: Class Calendar - Pointer To A Function Error

Nov 30, 2013

I have a class Calendar which has an attribute of priority queue, that accepts records of structure defined as:

typedef void (Calendar::*eventPointer)();
struct activationRecord {
double Time;
int Priority;
eventPointer activationEvent;
};

And here is the problem. Whole day I've been trying to fill the Calendar with some test entries by calling the method

void Calendar::calendarPush(double Time, int Priority, eventPointer event)

This is how I call it

calendar.calendarPush(Time, Priority, &Calendar::calendarEmpty);

But Visual Studio keeps to warn me with this error

argument of type "bool (Calendar::*)()" is incompatible with parameter of type "eventPointer *"

#include <iostream>
#include "Calendar.h"
using namespace std;
int main() {
cout << "Initializing ..." << endl;
double Time = 0.0;
int Priority = 0;

[Code] ....

View 4 Replies View Related

C++ :: Calling Function In Custom Class Using Pointer

Nov 6, 2013

Right now I have code

#include <iostream>
using namespace std;
class Rectangle {
private:
double width;
double length;

[Code] .....

it gives error ...

View 1 Replies View Related

Visual C++ :: Friend Class And Pointer Function

Mar 9, 2013

I need understanding this block of code, particularly this line : *getLeftChild() { return this - _child; }

Code:

public class UpperNode {
BOX _box;
int _child;
FORCEINLINE UpperNode *getLeftChild() { return this - _child; }
...
};

Here I have this function:

Code:
void
UpperNode::visulization(int level) {
if (isLeaf())
_box.visulization();
else
if ((level > 0)) {

[Code] .....

It also makes calls for "getLeftChild()";

But I see that getLeftChild expects function pointer, and I absolutely have no clue where "this" comes from inside function body.

(return this - _child) - "this" has to be integer.

Or, if we gave pointer, and "this" is referring to some UpperNode, then I can't understand to which one, I have no UpperNode array defined or something. So if this functions is actually scaling pointer address, then scaling where to? I could comprehend it, if I had some array of UpperNodes, but not just class. I have UpperNodes array defined in other friendly class, but don't think they are related .....

View 2 Replies View Related

C/C++ :: Using Class Parameter In Virtual Class

Mar 27, 2015

I am trying to create a platformer and is stuck on a problem regarding my virtual class Entity. I wish to use it to create stuff like the Player and Enemy class(es). But how to do the parameter for my collision check function. Below is my Entity- and player class.

There might be a better way to check CC with a lot of different objects, this is my first attempt.

This is the error I am getting: "error C2664: 'bool Player::CollisionCheck(Hostile)' : cannot convert argument 1 from 'Player' to 'Hostile'"

#ifndef ENTITY_H
#define ENTITY_H
#include <SFMLGraphics.hpp>
class Entity {
public:
Entity();
Entity(sf::Vector2f position, sf::Vector2f size, sf::Color fillColor, sf::Color outlineColor);

[Code] ....

and in Hostile I would (I guess) use
bool CollisionCheck(Player p);

But if I try for example to use Player in the CC in player.h it will complain that the function doesn't have an overload for that. Hostile is just a example class name right now, it isn't implemented yet. I am trying to use Player, but if possible wish to be able to have a different class depending on what kind of entity it is. The entity will probably also be the players projectiles and so on.

View 1 Replies View Related

C++ :: Using A Member Within Pointer Function (that Is Located In Same Class) - Segfault

Jan 6, 2013

I'm currently programming a server which uses multiple threads- I have a class for one map in the game. Each map has a thread for timed events(tile regeneration, NPC regeneration, etc.), and a thread for handling NPCs(movement, combat, etc.). A basic structure of the class looks like this:

class Region {
public:
/* game values are here, they are public so
they can be accessed from outside of the class
inside of packet-handling functions and such */
int value;
void *Function();

[Code] ....

The program crashes when I use a member of the same class the function is located in- in the context I have shown about it would crash on "value++".

View 11 Replies View Related

C++ :: Passing A Function Pointer As Template Argument To A Class

Aug 15, 2012

I have in the past written code for templated functions where one function argument can be either a function pointer or a Functor. Works pretty straightforward.

Now I am in a situation where I am actually trying to pass a function pointer as template argument to a class. Unfortunately this does not work, I can pass the Functor class but not the function pointer. Below code illustrates the issue:

Code:
#include <string>
#include <iostream>
#include <sstream>
#include <cstdlib>
// For demonstration
const char * external_library_call() {
return "FFFF";

[Code] .....

The idea is to have the definition of the Record class simple and readable and have a maintainable way to add auto-conversion functions to the class. So the lines I commented out are the desirable way how I want my code to look. Unfortunately I could not come up with any way that was close to readable for solving this.

View 3 Replies View Related

C++ :: How To Access Virtual Base Class

May 4, 2013

How can I access the virtual base class? This is a practice exercise from c++ primer plus 6.

The problem is that the name becomes No Name instead of the name specified when creating the gunslinger, I don't know how I can call the virtual base class explicitly

Output,

#ifndef PERSON_H_
#define PERSON_H_
#include <string>
#include <iostream>
#include <cstdlib>
using std::string;
class person

[code]....

View 7 Replies View Related

C++ :: Cast Base Class Pointer To Derived Class Pointer

Feb 6, 2015

I create an instance of a base class (not derived class) and assign it to base class pointer. Then, I convert it to a pointer to a derived class and call methods on it.

why does it work, if there is a virtual table?

when will it fail?

// TestCastWin.cpp : Defines the entry point for the console application.//

#include "stdafx.h"
#include <iostream>
class B
{
public:
B(double x, double y) : x_(x), y_(y) {}
double x() const { return x_; }

[Code] ....

View 14 Replies View Related

C++ :: Pointer Usage To Base Class Object In Vector Int Main Function

Aug 12, 2013

I know what are pointer's and how to use them but there is one point i am not able to understand. Below is the example code

I understand everything in the below code except 1 thing why i am using pointer to base class object in vector int the main() Function?

Code:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
// base class

[Code] ...

Here is the lines of code i want to understand.

Code:
vector<Employee*> employees;
employees.push_back(&emp1);
employees.push_back(&mgr1);

I know if i will not use the pointer base class function "virtual double grossPay" will be called for both base class object and derived class object and when i will use pointer with reference to the object because base class function is virtual it will look for same function in derived class and if available it will execute it.

View 3 Replies View Related

C++ :: Graph Class - How To Provide Virtual Iterators

May 29, 2013

I have a 'Graph' class, which has derived classes for Adjacency Matrix and Adjacency List representations.

How do I provide iterators for traversing vertices and edges, when the iterator classes would have different implementations for the different derived classes ?

The following way is the only one I can think of, but seems quite cumbersome.

Code:
class Base {
public:
class BaseIterator {

};
virtual const BaseIterator& begin();
virtual const BaseIterator& end();

[Code] .....

Or is there a pattern for doing this that I'm not aware of ? Would composition be a better idea here compared to polymorphism ? I mean, I can think like..a Graph can 'have' several representation 'objects' within it.

All the involved classes are templates,not sure if that makes the situation different.

View 7 Replies View Related

C++ :: Overriding Virtual Operator Of Parent Class

Mar 20, 2013

Below is simplified code consists of two classes, namely Parent and Child.

Child is inherited from Parent.

All member functions of class Parent are declared virtual, and they have been overridden in the class Child.

Code 1:

#include <cstdlib>
#include <iostream>
using namespace std;
#define QUANTITY 5
class Parent {

[Code] ....

The output of the code:

Child::showID() -- ID is 1804289383
Child::showID() -- ID is 846930886
Child::showID() -- ID is 1681692777
Child::showID() -- ID is 1714636915
Child::showID() -- ID is 1957747793

Parent::operator=() invoked.

Child::showID() -- ID is 1804289383
Child::showID() -- ID is 846930886
Child::showID() -- ID is 1714636915
Child::showID() -- ID is 1714636915
Child::showID() -- ID is 1957747793

Question:

Why is Parent::operator= invoked instead of Child::operator= ..?

Isn't it already declared virtual and hence would be overridden..?

I need to invoke Child::operator= instead. How to achieve this?

View 12 Replies View Related

C++ :: Copying Purely Virtual Class Child

Oct 27, 2014

In short, this is what I have

class A{
A(){}
virtual void pure() = 0;
}

[Code] .....

I need a2 to be a deep copy of a1, but if I understand it correctly, then a2 should just be a pointer copy of a1. How do I make a2 be a different instance of B?

View 5 Replies View Related

C++ :: Pure Virtual Methods And Interface Class

Jul 11, 2012

I develop add-ons for MS Flight Simulator and I use the poorly documented SDK for this. The SDK provides a .h file in which an interface class is defined (with pure virtual methods only), something like this:

Code:
class IPanelCCallback {
public:
virtual IPanelCCallback* QueryInterface (PCSTRINGZ pszInterface) = 0;
virtual bool ConvertStringToProperty (PCSTRINGZ keyword, SINT32* pID) = 0;
virtual bool ConvertPropertyToString (SINT32 id, PPCSTRINGZ pKeyword) = 0;
};

In my code, I use this interface like this:
Code:
IPanelCCallback* pCallBack = panel_get_registered_c_callback("fs9gps");
...
SINT32 id;
pCallBack->ConvertStringToProperty(propertyName, &id);

Everything works fine, but I don't understand why... I thought the linker would stop with an "undefined symbol" error because the IPanelCCallback methods, such as ConvertStringToProperty, are declared as pure virtual but defined nowhere, and I don't use any library for linking. With such an interface class, I thought I would have to defined a subclass of IPanelCCallback and define the ConvertStringToProperty method.

View 6 Replies View Related

C/C++ :: Couldn't Find Index Of Array

Feb 12, 2014

I have problem with find the index of the following array.

int minimums[2]={201,201};//first initialize both to greater than maximum allowed

for (int index = 0; index < 200; index++) {
if(find_array[index] < minimums[0]) {
minimums[0] = find_array[index]; //Lowest number

[Code]....

View 14 Replies View Related

C++ :: Why Do Virtual Enums Compile When They Cannot Be Defined In Derived Class

Aug 26, 2013

Assume this class:

class GenericTrafficLight {
public:
virtual enum LightState;
void setLightState(LightState newState) {
currentState = newState;
}
private:
LightState currentState;
};

And this deriving class:

class FuturisticTrafficLight : public GenericTrafficLight {
public:
enum LightState {
LIGHT_STATE_RED = 0,
LIGHT_STATE_YELLOW = 1,
LIGHT_STATE_CYAN = 2,
LIGHT_STATE_GREEN = 3
};
};

This yields this error: "C2911 (...) cannot be declared or defined in the current scope" in the deriving class's enum definition.

View 5 Replies View Related

C++ :: Unable To Start Program - System Couldn't Find The File

Jan 2, 2013

Why do i get this error?

View 4 Replies View Related

C++ :: Alternative Of Virtual Function?

Sep 25, 2013

have a look-

class Base
{
private:
{

[Code].....

my question is why we can't directly call the member function of the desired class instead of using virtual function.

***********same program using virtual keyword*******************

class Base
{
private:
{

[Code].....

Why we generally prefer the 2nd one i.e with virtual keyword. why we can't directly call the member function of the desired class instead of using virtual function...make me understand this point..

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved