passing class object to pure virtual fkt

G

Gernot Frisch

I want to have a class that provides 2 methods with the same name
(do1, do2) that cann be called from a function (Fkt) but Fkt does not
know/care about it's type.


class base
{
public:
virtual void Do(base& b)=0;
};

class do1:public base
{
public:
void Do(base& d) {printf("%d", (do1)d.magic);}
long magic;
};

class do2:public base
{
public:
void Do(base& d) {printf("%d", (do1)d.othermagic);}
long othermagic;
};


void Fkt(base& cl)
{
cl.Do(cl);
}


--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
 
G

Gianni Mariani

Gernot said:
I want to have a class that provides 2 methods with the same name
(do1, do2) that cann be called from a function (Fkt) but Fkt does not
know/care about it's type.

I must be slow this morning. Do you ming clarifying your question ?
 
M

Moonlit

Hi,

Gernot Frisch said:
I want to have a class that provides 2 methods with the same name (do1,
do2) that cann be called from a function (Fkt) but Fkt does not know/care
about it's type.


class base
{
public:
virtual void Do(base& b)=0;
};

class do1:public base
{
public:
void Do(base& d) {printf("%d", (do1)d.magic);}
long magic;
};

In your posts I always see you pass the base class in the derived object.
This isn't normally necessary. Don't forget inheritance is NOT a
child->parent relation ship. The derived object IS the base class and the
derived class. so that would make the Do function:

#include <iostream>
class base
public:
virtual void Do()=0;
};
class do1:public base
{
public:
virtual void Do() {std::cout << magic << std::endl; }
long magic;
};
class do2:public base
{
public:
virtual void Do() {std::cout << othermagic << std::endl;}
long othermagic;
};

(The virtual in the derived has actual no effect but it, shows that the
function is a virtual to anyone reading the code).
 
G

Gernot Frisch

Oh. My problem is another:
I don't want to pass 'this' to the function, but another instance of
the class. Such as:

class Crayons : public ColorfulPens
{
void SetMeTheColorOf(const Crayon& anotherCrayon);
}

Is it clearer now?
Sorry for being unable to express my problems.
-Gernot
 
M

Moonlit

Hi,

I suppose you want to do this and set Crayons color also from ColorfullPens
(I also assume the Crayon in the function should be Crayons ),.

class CColor{};
class ColorfulPens
{

CColor Color;
public:
const CColor& GetColor() const { return Color; }
void SetColor( const CColor& Color ) { this->Color = Color; }
};
class Crayons : public ColorfulPens
{
public:
void SetMeTheColorOf(const ColorfulPens& Pen )
{
SetColor( Pen.GetColor(); }
}
};

Regards, Ron AF Greve
 
G

Gernot Frisch

Moonlit said:
Hi,

I suppose you want to do this and set Crayons color also from
ColorfullPens (I also assume the Crayon in the function should be
Crayons ),.

class CColor{};
class ColorfulPens
{

CColor Color;
public:
const CColor& GetColor() const { return Color; }
void SetColor( const CColor& Color ) { this->Color = Color; }
};
class Crayons : public ColorfulPens
{
public:
void SetMeTheColorOf(const ColorfulPens& Pen )
{
SetColor( Pen.GetColor(); }
}
};

Now, make

class WaxCrayon:public ColorFulPens
{
public:
void SetMeTheColorOf(const ColorfulPens& Pen)
{
// now please cast Pen to type: WaxCrayon to access other
member functions
}
};


I'll explain my situation in detail, so it might give a better
approach:

I have a parser class that converts:
a=3+3/6

to:

3
a=3+-
6

In ASCII. Now I want to do the same using e.g. GDI drawings, for nicer
displaying of roots e.g.

The class ASCIIblock has these methods:

void Set(const char* singleline);
void InsertAt(long x, long y, const ASCIIblock& block_to_paste);
void MakeDivision(const ASCIIblock& divident, const ASCIIblock&
divisor);
....

The parser class has a function:

template class <DRAWblock>bool EvaluateGFX(DRAWblock& out)
{
...
DRAWblock block1, block2;
block2.Set(someline);
block1.InsertAt(0,0, block2);
...
}

and get's called with:
ASCIIblock ab;
MyClass.EvaluateGFX(ab);

But I need a class that has these functions:
class GDIblock
{
public:
void Set(const char* singleline);
void InsertAt(long x, long y, const GDIblock& block_to_paste);
void MakeDivision(const GDIblock& divident, const GDIblock&
divisor);
};

....er... wait... that's working isn't it? I have to re-think about my
problem myself now... (If it still exists)

-Gernot
 
M

Moonlit

Hi,

I think I understand your problem better now. So you have a parser class
that can draw itself on the console by passing it a "console" object. Now
you want to let the parser class draw in a window so you want to pass it a
GDI object. Actually what you would like to do is pass the parser class some
generic interface to draw blocks, characters lines etc. Only the
implentation (in the derived class) knows how it is going to do that i.e. on
a ASCII screen or in a GDI context (which in itself is an abstraction for
printer, screen etc.) or anything else hence:


class CContext
{
public:
virtual void DrawText() = 0;
.... DrawLine etc.

};
class CGDIContext
{
private:
HDC DC;
public:
CGDIContext( HDC DC ):
DC( DC){}

virtual void DrawText()
{
// use TextOut etc to draw tree in device context
}
.... DrawLine etc.
};
class CASCIIContext
{
public:
virtual void DrawText()
{
// Use printf to draw tree on console
}
.... DrawLine etc.
}

class CParser
{
private:
CParseTreee Tree;
public:
void DrawMyself( CContext& Context )
{
// Draw the tree using the abstract line text etc of Context.
Context.DrawText( Tree.GetNode() ); // or something like that
}
};

CParser Parser;

Parser.Parse( file);
CGDIContext GDIContext( GetWindowDC() );
Paser.DrawMyself( GDIContext );
CASCIIContext ASCIIContext;
Paser.DrawMyself( ASCIIContext );


Regards, Ron AF Greve.

you want to make that parser class a
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top