473,382 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

passing class object to pure virtual fkt

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
Jul 22 '05 #1
6 1594
Gernot Frisch wrote:
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 ?


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);
}

Jul 22 '05 #2
Hi,

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2q************@uni-berlin.de...
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).


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

Jul 22 '05 #3
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
Jul 22 '05 #4
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

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2q************@uni-berlin.de...
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

Jul 22 '05 #5

"Moonlit" <news moonlit xs4all nl> schrieb im Newsbeitrag
news:41***********************@news.xs4all.nl...
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



Jul 22 '05 #6
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
"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2q************@uni-berlin.de...

"Moonlit" <news moonlit xs4all nl> schrieb im Newsbeitrag
news:41***********************@news.xs4all.nl...
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


Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bonj | last post by:
Hello Can anyone assist with the following class hierarcy problem? I have a series of window classes, the object model currently being as such: Window / | \ / | \...
4
by: WittyGuy | last post by:
Hi all, Though I know the concepts of both abstract class & virtual function (like derived class pointer pointing to base class...then calling the function with the pointer...), what is the real...
5
by: Luke Dalessandro | last post by:
Code: Thread -> U -> T public class Thread { protected: thread_t _tid; virtual void foo() = 0; public: // Static entry function for the internal thread
8
by: Dev | last post by:
Hello, Why an Abstract Base Class cannot be instantiated ? Does anybody know of the object construction internals ? What is the missing information that prevents the construction ? TIA....
22
by: ypjofficial | last post by:
Hello All, I have following doubt.. class abstractclass { public: abstractclass(){} virtual void method()=0; };
4
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not...
12
by: sojin | last post by:
Hi all, I'm a new to c++... and I've some doubts on "virtual" topics. 1) Can abstract base class have V-table? Here's the way,, Class CTemp{
6
by: MattWilson.6185 | last post by:
Hi, I'm trying to find out if something is possible, I have a few diffrent lists that I add objects to and I would like to be able to have a wrapper class that won't affect the internal object, for...
3
by: nw | last post by:
Hi, I have three classes, a template pure virtual base class, a template derived class and a third which I would like to use to store copies of the derived class. The code looks like this: ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.