static method returning inherited type

D

Dan

I have class B and C which inherit from class A.
I have a static method:

A* aRequest(unsigned char *byte_buffer, size_t length)
{
A *foo;

if(something == true)
{
foo = new B;
return foo;
}
else
{
foo = new C;
return foo;
}
}

So that when the method aRequest is called from my parser it should
return a pointer to one of the subclass types B or C.

Problem is when I try to use this:

XMLRequest *foggy;
foggy = A::aRequest(buffer, len);
foggy->someOtherBMethod();

It says the referenced object "someOtherMethod" is not a member of
class A. Can anybody help me out here? I'm not really sure how to do it
properly.
 
F

Fred Zwarts

Dan said:
I have class B and C which inherit from class A.
I have a static method:

A* aRequest(unsigned char *byte_buffer, size_t length)
{
A *foo;

if(something == true)
{
foo = new B;
return foo;
}
else
{
foo = new C;
return foo;
}
}

So that when the method aRequest is called from my parser it should
return a pointer to one of the subclass types B or C.

Problem is when I try to use this:

XMLRequest *foggy;
foggy = A::aRequest(buffer, len);
foggy->someOtherBMethod();

It says the referenced object "someOtherMethod" is not a member of
class A. Can anybody help me out here? I'm not really sure how to do it
properly.

It is not clear what you mean.
Is someOtherBMethod the same as someOtherMethod?
Are these methods of class A, or of class B?
Is XMLRequest the same as A?
Note that you cannot call methods of class B with a pointer to a class A object.
If you want to access a method of class B with a pointer to a class A object,
you could try to use dynamic_cast to convert the foggy pointer to an other pointer
which the points to an object of class B.

Fred.Zwarts.
 
D

Dan

Fred said:
It is not clear what you mean.
Is someOtherBMethod the same as someOtherMethod?
Are these methods of class A, or of class B?
Is XMLRequest the same as A?
Note that you cannot call methods of class B with a pointer to a class A object.
If you want to access a method of class B with a pointer to a class A object,
you could try to use dynamic_cast to convert the foggy pointer to an other pointer
which the points to an object of class B.
Sorry for not being clear.
someOtherBMethod is a method that is only in the subclass B, with no
equivalent in A.
A* aRequest(...) isn't in either A or B it's a static method in another
(my parser) class.
XMLRequest is A, that was a c&p error on my part. whoops.

I'm trying to create a static method that will return a pointer to a
subclass of A object, ie either B or C. I'm just unsure of how to
handle it.
 
V

Victor Bazarov

Dan said:
Sorry for not being clear.
someOtherBMethod is a method that is only in the subclass B, with no
equivalent in A.
A* aRequest(...) isn't in either A or B it's a static method in
another (my parser) class.
XMLRequest is A, that was a c&p error on my part. whoops.

I'm trying to create a static method that will return a pointer to a
subclass of A object, ie either B or C. I'm just unsure of how to
handle it.

You're doing it right. However, what to do with that pointer _after_
you have got it from that function seems to present a mystery even to
you. Why do you have the base class? Why are you returning a pointer
to the base class? Doesn't the caller of 'A::aRequest' care what type
the actual object has? If it doesn't, read up on polymorphism. If it
does, why don't you return a pointer to the class the caller needs?

V
 
F

flaviu

Dan said:
I have class B and C which inherit from class A.
I have a static method:

A* aRequest(unsigned char *byte_buffer, size_t length)
{
A *foo;

if(something == true)
{
foo = new B;
return foo;
}
else
{
foo = new C;
return foo;
}
}

So that when the method aRequest is called from my parser it should
return a pointer to one of the subclass types B or C.

Problem is when I try to use this:

XMLRequest *foggy;
foggy = A::aRequest(buffer, len);
foggy->someOtherBMethod();

It says the referenced object "someOtherMethod" is not a member of
class A. Can anybody help me out here? I'm not really sure how to do it
properly.

Is someOtherBMethod() part of XMLRequest (aka A) public interface?
Suppose not, given the compile error.
I guess you've figured out a solution to the problem by now.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top