stuck on operator overloading???

M

MM

Hi,

class A
{
protected int anum;
}
class B : A
{
public static B operator ++(B b_class)
{
b_class.anum++;
return b_class;
}
}
class C : B
{
static void Main(string[] args)
{
C c_class = new C();
c_class.anum = 10;
c_class++; // ERROR - cannot implicitly covert type - are
you missing a cast?
}

Hi, I'm learning. I want to increment a value type buried in a class
hierarchy. I thought that B descendants would inherit default action of ++
defined in Class B but doesn't work. From my reading, it seems that most
operator overloads create a new object and return that but I don't want to
do this. I simply want a value incremented in the base class to be the
standard behaviour in all derived classes. I can sortof understand the
error - the ++ operator returns a class B which can't be recast to a class
C - what do I do? Thanks alot for your help! matt.
 
B

Ben Voigt [C++ MVP]

MM said:
Hi,

class A
{
protected int anum;
}
class B : A
{
public static B operator ++(B b_class)
{
b_class.anum++;
return b_class;
}
}
class C : B
{

public static C operator++(C c_class)
{
B b_class = c_class;
b_class++;
return c_class;
}

static void Main(string[] args)
{
C c_class = new C();
c_class.anum = 10;
c_class++; // ERROR - cannot implicitly covert type - are
you missing a cast?
}

Hi, I'm learning. I want to increment a value type buried in a class
hierarchy. I thought that B descendants would inherit default action of ++
defined in Class B but doesn't work. From my reading, it seems that most
operator overloads create a new object and return that but I don't want to
do this. I simply want a value incremented in the base class to be the
standard behaviour in all derived classes. I can sortof understand the
error - the ++ operator returns a class B which can't be recast to a class
C - what do I do? Thanks alot for your help! matt.

hmmm, did you try a generic:

class B : A
{
public static T operator ++<T>(T b_class) where T : B
{
b_class.anum++;
return b_class;
}
}
 
M

MM

Thanks for all the help Ben and Pete.
Ben Voigt said:
MM said:
Hi,

class A
{
protected int anum;
}
class B : A
{
public static B operator ++(B b_class)
{
b_class.anum++;
return b_class;
}
}
class C : B
{

public static C operator++(C c_class)
{
B b_class = c_class;
b_class++;
return c_class;
}

static void Main(string[] args)
{
C c_class = new C();
c_class.anum = 10;
c_class++; // ERROR - cannot implicitly covert type - are
you missing a cast?
}

Hi, I'm learning. I want to increment a value type buried in a class
hierarchy. I thought that B descendants would inherit default action of
++ defined in Class B but doesn't work. From my reading, it seems that
most operator overloads create a new object and return that but I don't
want to do this. I simply want a value incremented in the base class to
be the standard behaviour in all derived classes. I can sortof understand
the error - the ++ operator returns a class B which can't be recast to a
class C - what do I do? Thanks alot for your help! matt.

hmmm, did you try a generic:

class B : A
{
public static T operator ++<T>(T b_class) where T : B
{
b_class.anum++;
return b_class;
}
}
 

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

Top