473,387 Members | 1,606 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,387 software developers and data experts.

how get instanace-class-name of a variable

hi,

i've got the following classes:

class CImage {}
class CImageBlackWhite : public CImage {}
class CImageColor : public CImage {}

and i've got a global function
void function_name (CImage * image) {

if image is instace of CImageBlackWhite {
do something
}

else if image is instance of CImageColor {
do something else
}

return;
}

my question/problem:

i don't know how to get the name of the class from the variable image.
thanks!!

Dec 11 '06 #1
2 1208

unknown; wrote:
hi,

i've got the following classes:

class CImage {}
class CImageBlackWhite : public CImage {}
class CImageColor : public CImage {}

and i've got a global function
void function_name (CImage * image) {

if image is instace of CImageBlackWhite {
do something
}

else if image is instance of CImageColor {
do something else
}

return;
}

my question/problem:

i don't know how to get the name of the class from the variable image.
Look up Liskov Substitution Principle (spelling probably not correct)
to find out why this is bad design. Look up dynamic_cast and the
typeof operator to learn how to do it.
>

thanks!!
Dec 11 '06 #2

unknown; wrote:
hi,

i've got the following classes:

class CImage {}
class CImageBlackWhite : public CImage {}
class CImageColor : public CImage {}

and i've got a global function
void function_name (CImage * image) {

if image is instace of CImageBlackWhite {
do something
}
CImageBlackWhite * cibw = dynamic_cast<CImageBlackWhite*>(image);
if(cibw) {
do something
}
else if image is instance of CImageColor {
do something else
}
CImageColor* cic = dynamic_cast<CImageColor*>(image);
if(cic) {

return;
}
However, Noah is right in his other post; this is a bad code smell.
There is often a better approach. The typical method is to add a
virtual method to CImage. CImageBlackWhite::foo (where foo is the new
virtual method) would then "do something" and CImageColor::foo would
"do something else". Then function_name would just call image->foo().

But unfortunately, this is not always possible to do. In particular, if
the CImage heirarchy is not under your control, you can't change it.

Evan

Dec 11 '06 #3

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

Similar topics

10
by: patang | last post by:
Question 1 I have two forms, let say Form1 and Form2. There is datetimepicker control on Form2. In the Load Event of Form2 I have set the default value of datetimepicker control to NOW. So as...
2
by: John H | last post by:
Hi, How can i just use the XmlDocument object to validate an xml instanace against a schema referenced inside the xml instance? The Load method seems to not validate it against the schema. ...
1
by: sukumaster | last post by:
I can display full table in a griedview but how can i display only somecolumns in a table and how to display some text for an instanace 'Itemmaster' in a datagrid
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.