473,396 Members | 2,020 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,396 software developers and data experts.

static method returning inherited type

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
4 1388
"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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Yu | last post by:
I have found that the static object is initialised at the time when the shared libary is loaded. The initialisation caused the invocation of the constructor. May I know of any way that I can...
19
by: Mike Ruane-Torr | last post by:
Why can't I have a static abstract method in C#? My intention is to have a class-level method that returns a string to supply information about inherited classes, and it is natural to make this...
4
by: Bryan Green | last post by:
So I'm working on a project for a C# class I'm taking, where I need to keep some running totals via static variables. I need three classes for three different types of objects. The base class and...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
5
by: Eliseu Rodrigues | last post by:
Hi I would like to have a static method on a base class that executes some action (for example retrieves the row count) on a table whose name is the same of the inherited class name. For...
4
by: mfc | last post by:
How do i get the type info in a static method? for instance in the code below is it possible for the Method to get the type to know what type was used to call Method? thanks class C {
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
4
by: Andrus | last post by:
I tried to use type parameter to call static method but got compile error 'T' is a 'type parameter', which is not valid in the given context. Why this is not allowed ? Should I really use a lot...
14
by: d-42 | last post by:
Hi, Is there any way to make this method inheritable and have it behave appropriately on inherited classes? Using generics? Extension methods? Something else? class baseClass { public...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.