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


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# :: Open Source Library For Graph Drawing (alternative To GraphX)

Feb 25, 2015

Is there any open-source graph visualization library like GraphX or Graph#? I need a library which lets me easily draw graphs, vertices and edges and apply various layouts to them. The support for GraphX and Graph# is really bad and their forum doesn't work ...

I dont want to do any advanced thing, just to create graphs and applying layout to them for maximum of 20 nodes.

I have tried Yworks, which is a commercial tool, but that one is way too expansive ...

View 3 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++ :: Define To Overload Virtual Function

Jul 11, 2014

I want to overload pure virtual function from 3rd party SDK class to put my debug messages like that:

errorStatus aXsaction(){printf(_T("
abort transaction"));transactionManager->abortTransaction();}

#define transactionManager->abortTransaction() aXsaction()

But compiler complains on the minus sign:
error C2008: '-' : unexpected in macro definition

Is it possible to trick the compiler?

View 4 Replies View Related

C++ :: Overload Virtual Member Function In Polymorphism?

Nov 30, 2013

I defined a virtual class and three other classes based on it. I want to use them like this:

int main() {
Dirichlet_t D;
Neumann_t N;
Cauchy_t C;

PDEBoundary_t * B1=& D;
PDEBoundary_t * B2=& N;
PDEBoundary_t * B3=& C;

[Code] .....

but I got two major errors
1: "object f abstract type is not allowed" error.-----why not?
2: "the derived class must implement the inherited pure virtual method"-----Did't I?

View 15 Replies View Related

C++ :: Virtual Function Defined / How To Define A Destructor

Aug 22, 2014

I wrote the following program, it can be compiled and run, but there is warning saying that if virtual function is defined, there should be a destructor. How to do that I tried many different ways I can thought of, but none of them works.

#include <iostream>
using namespace std;
class cell_c {
public:
double p;
cell_c() {p=1;}
virtual void print() {cout<<p<<endl;}

[code]....

View 1 Replies View Related

C++ :: Pure Virtual Function Called At Runtime

Jul 12, 2014

I'm currently making a game and what happens is that during runtime, it suddenly closes and a message is shown in the console saying "Pure virtual function called at runtime".

Here is the code: [URL]

The problem seems to occur somewhere between lines 662 - 695. And it seems to only happen when the size of the vector reaches 1.

View 2 Replies View Related

C++ :: Define Virtual Function Compare In Subclass Score?

Mar 26, 2014

I am trying to bend my head around polymorphism but I can't figure out how to use the virtual method properly. I have the following superclass:

Code: class comparable
{
public:
comparable();
virtual bool compare(comparable &other)=0;
}; and the subclass:
Code: class score : public comparable
{
public:
score(string player,int highscore);
bool compare(comparable &other);
string name;
int highscore;
};

My problem is that I do not know how to define the virtual function compare in the subclass score:

Code: bool score::compare(comparable &other){
if(this->highscore > other.???){
...
}
}

How do I tell the compiler that the type is of score?

View 7 Replies View Related

C++ :: Changing Virtual Function Output Without Using Any Data Member

May 10, 2014

Instead of this:

#include <iostream>
struct Object {
int size; // Want to avoid this because size is (almost always) constant
Object (int s): size(s) {} // for every Object subtype.

[Code] ....

I want this:

#include <iostream>
struct Object {
virtual int getSize() const = 0;
};
struct Block: Object {
int getSize() const {return 5;} // always 5, except once in a blue moon it may change

[Code] ....

The Decorator Pattern works (getSize() can then return 6) but it is a poor choice for my program because it will cause more problems (due to many containers holding its old address, among other things. Any way to achieve this without any change of address, and without creating new storage somewhere, either inside the class or outside the class (too much responsibility to follow that stored value for the rest of the program just for this rare change, and creating a data member uses up too much memory for the many, many Block instances)?

View 5 Replies View Related

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 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++ :: Virtual Can Only Exist In Non-static Member Function - Field Has Incomplete Type Void

Dec 5, 2014

I'm writing a class "Property" for a program that manages different types of properties. This is my .h for y base class. I was trying to write a virtual void function to convert different children classes to strings that can be displayed, but Xcode is freaking out.

I had it as:

virtual void toString()= 0;

and it gave me an error message: "Virtual can only exist in non-static member functions" and "field has incomplete type 'void'"

I changed it to:

virtual string toString() = 0;

and the error message didn't change.

Is this an issue with Xcode or did I do something wrong? Even after changing it to string it told me that it "has incomplete type 'void'"....

View 1 Replies View Related

C++ ::  Virtual Methods With MI

Jan 6, 2014

I have questions about multiple inheritance and virtual methods. I have a class called solid. All objects of this class have hitboxes and can collide with others. I have the following methods:

void testCollision(something begin, something end);
/* This method takes a container's begin and end iterators to test if the object collides with any other object of the list of all the solids currently in the game area. Each time there is a collision, it calls collide(other) and other.collide(*this) */

virtual bool collide(solid& other);
/* This method always returns false and does nothing */

This class will be inherited by another class which will have overloads for a few specific collisions. For example:

class player : public solid{
public:
bool collide(projectile& other);
bool collide(enemy& other);
bool collide(wall& other);
};

My question is quite simple actually. If I have a loop which calls testCollision() with all elements in the list of all solids (a list of pointers to solids to be exact) and there is a collision between the player and a projectile, will testCollision call player::colide(projectile& other) or will it call solid::collide(solid& other). And in any case, did I understand how to use the virtual keyword? If I'm right, it should call the player::colide method if it's there for the specific type, else it will call the solid::colide which only returns 0, ignoring collision.

View 4 Replies View Related

C++ :: Use Of Virtual Keyword In Destructors

Jan 6, 2014

have a look at the following code :

class Base
{
public:
virtual ~Base()
{
cout << "Calling ~Base()" << endl;

[Code]...

Now this program produces the following result:

Calling ~Derived()
Calling ~Base()

i was reading online and got stuck here. i am unable to understand why 'calling ~Base()' is been printed here? when we reached delete pbase; in int main() it goes to Base class first and finds that its destructor is virtual so it goes to Derive class and finds another destructor and executes it but why does it prints ~Base() in any case?

View 4 Replies View Related

C++ :: Using Reference With Virtual Method

Mar 19, 2013

#include <iostream>
using namespace std;
struct A {
virtual void f() { cout<<"A
"; }
};

[code]...

I would expect that both examples 2 & 3 will give me the same result.I tried to figure it out but I could not. Both are references of a base class type, that get a derived object.

Q1 : why is the difference between them ?

As I see it, its kind of a mix between pointer - which in case of virtual method that was override in derived class - would give me the derived method (e.g. "B") and between regular object - which in case of virtual method that was override - would give me the specific method (Still "B"). So, example 2 "use" it as a regular object and example 3, "use" it as pointer.

Q2 : How should I refer to it ? I am using VS2008.

View 12 Replies View Related

C++ :: Virtual Keyword In Inheritance?

Feb 19, 2014

How to put virtual keyword in the function of the base class. Will the function still be over-written properly? What will happen if I

(1) call function from base class pointer
(2) call function from derived class pointer
(3) call function from derived class object

If the virtual keyword is present, the over-written version will be called in all cases, am I mistaken?

View 1 Replies View Related

C++ :: Why CRTP Used Instead Of Virtual Functions

Jan 31, 2014

I was reading about the CRTP, and I can't for the life of me understand it's advantages over virtual functions.

Unless you're coding embedded systems, and can't afford the few extra bytes for the vptr, or coding something requiring high-performance, where every nanosecond counts, I just don't see why the CRTP is so attractive. It just adds more text and forces every user class that wants to use the CRTP'd hierarchy to become a template class.

I tried implementing my Functor hierarchy with the CRTP instead of virtual functions...All it did was clutter my files with angle brackets and made the whole thing look very ugly.

View 14 Replies View Related

C++ :: Virtual Breaks Encapsulation?

Jul 17, 2013

Here is the code,

Code:
class A {
public:
virtual void foo(){}
};
class B : public A {

[Code] ....

Obviously when you call pA->foo, it will call foo defined in B. But foo defined in B is private. It is not supposed to be called outside the class B. So it looks like virtual breaks encapsulation.

View 8 Replies View Related

C++ :: Virtual Functions And Polymorphism

May 2, 2014

if we do a virtual functions(polymorphism) why we need re-declare the functions(when we create a new class derived from other)?

View 10 Replies View Related

C++ :: Make A Sort Of Virtual Pet Game

Dec 29, 2014

I'm just playing around with C++ and I'm trying to make a sort of virtual pet game. I know its not the best looking code but I'm trying. But as of right now I'm trying to be able to animate the game a little . For example , if a user decides to "pet" their "pet" I want the pets mouth to open from '_' to ^o^ like this:

() ()
('_') -> () ()
(^o^) and switch back and forth.

Here's my code:

#include <iostream>
#include <cstring>
using namespace std;
void Greeting()

[code]....

View 5 Replies View Related

C++ :: Cannot Change Virtual Functions From Object?

Oct 14, 2013

I try the functions pointers too, but without success. I understand the objects are the way for work with class's. until here fine. But why i can't change the virtual functions from an object? is there anyway for do it? Ican't do, outside of classfunctions, these code:

#include <iostream>
using namespace std;
class test {
public:
virtual void created(){};

[Code] ....

How i can validate these line:

void a::created()
???

View 2 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++ :: Overriding Of Overloaded Virtual Member

Feb 5, 2013

I've got the following code with output. I can't figure out myself why it's what printed out there. I believe, it has something to deal with overloading/overriding/virtual functions implementations in C++:

class Base{
public: virtual void f(int);
virtual void f(double);
}

[Code].....

Thus here're my conclusions:
1) in line
d.f(1.0);
for some reason compiler preferred casting double->int of the argument and then call to 'Derived::f(int)'.

2)in line
pb->f(1.0);
for some reason compiler preferred call to 'Base::f(double);'. 'Base' is static type of pb, but the dynamic type is 'Derived'.

I believe the answer has to deal with the fact whether virtual table contains in addition to functions' names also the types of arguments they accept. AFAIK, vTable doesn't include such info.

View 7 Replies View Related

C++ :: Using Virtual Functions In Base Classes

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

C++ :: Overriding Inherited Virtual Functions

Feb 15, 2013

Is it possible to do something like this:

class A //parent {
public:
virtual void DoSomething() = 0;
};

class B : public A //child {
public:
void DoSomething(string s) override;
}

Where the child member function overrides and changes the parents member function.

I need to pass an array of key states to the Controller class' Update() function but don't want to send it to every class derived from Object (like Controller).

Is this possible or do I have to overload the original Update() member function (but I would need to define the method in Object then (i.e remove the pure virtual function (=0)))

View 4 Replies View Related







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