By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

static method returning inherited type

P: n/a
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.

Jun 21 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Dan" <da**********@gmail.com> wrote in message news:11*********************@m73g2000cwd.googlegro ups.com...
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.
Jun 21 '06 #2

P: n/a
Dan
Fred Zwarts wrote:
"Dan" <da**********@gmail.com> wrote in message news:11*********************@m73g2000cwd.googlegro ups.com...
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.

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.

Jun 21 '06 #3

P: n/a
Dan wrote:
Fred Zwarts wrote:
"Dan" <da**********@gmail.com> wrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
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.

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 21 '06 #4

P: n/a

Dan wrote:
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.

Jun 21 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.