I have a base class (B) and several derived classes (D1, D2, ...)
I have a container class (C) with an attribute (void *d) which is
intended to point to any of D1, D2,...
My question is, on my C::GetObject() accessor, what is the most
appropriate return type?
void* C::GetObject( void ) { return d; }
or
B* C::GetD( void ) { return (B*) d; }
Is there any galvanizing rationale as to why I'd choose one return
type over the other, or is there yet another possibility I have not
considered?
TIA
Brian Herbert Withun 5 1543
On Aug 20, 12:52 pm, Brian Withun <bhwit...@gmail.comwrote:
I have a base class (B) and several derived classes (D1, D2, ...)
I have a container class (C) with an attribute (void *d) which is
intended to point to any of D1, D2,...
My question is, on myC::GetObject() accessor, what is the most
appropriate return type?
void*C::GetObject( void ) { return d; }
or
B*C::GetD( void ) { return (B*) d; }
Is there any galvanizing rationale as to why I'd choose one return
type over the other, or is there yet another possibility I have not
considered?
TIA
Brian Herbert Withun
[[disregard the C::GetD() above, my intended illustration is below]]
void* C::GetObject( void ) { return d; }
or
B* C::GetObject( void ) { return (B*) d; }
"Brian Withun" <bh******@gmail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
>I have a base class (B) and several derived classes (D1, D2, ...)
I have a container class (C) with an attribute (void *d) which is
intended to point to any of D1, D2,...
My question is, on my C::GetObject() accessor, what is the most
appropriate return type?
void* C::GetObject( void ) { return d; }
or
B* C::GetD( void ) { return (B*) d; }
Is there any galvanizing rationale as to why I'd choose one return
type over the other, or is there yet another possibility I have not
considered?
Your container class shoudl have an attribute of Base *d instead of a void*.
And, yes, return a B*
On Aug 20, 11:52 am, Brian Withun <bhwit...@gmail.comwrote:
I have a base class (B) and several derived classes (D1, D2, ...)
I have a container class (C) with an attribute (void *d) which is
intended to point to any of D1, D2,...
Why isn't it a B*? If D1, D2, etc derive from B, make it a B*.
>
My question is, on my C::GetObject() accessor, what is the most
appropriate return type?
If you want ot be able to store things that are not always derived
from B, then another possibility is to make C a template class.
template<typename T>
class C
{
private:
//Some type of data structures to store multiple instances of T.
std::list<T*internal_list; //example
std::vector<T*internal_vec; //example
public:
T* GetObject();
};
Then you would create instances of C as follows:
C<D1first;
D1* d1 = first.GetObject();
C<D2second;
D2* d2 = second.GetObject();
C<NotDerivedFromBthird;
NotDerivedFromB* ndfb = third.GetObject();
Jim Langston wrote:
"Brian Withun" <bh******@gmail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
>I have a base class (B) and several derived classes (D1, D2, ...)
I have a container class (C) with an attribute (void *d) which is intended to point to any of D1, D2,...
My question is, on my C::GetObject() accessor, what is the most appropriate return type?
void* C::GetObject( void ) { return d; }
or
B* C::GetD( void ) { return (B*) d; }
Oh, an there also a style mistake here. Please, drop the 'void'
between parentheses, it's so C.
>Is there any galvanizing rationale as to why I'd choose one return type over the other, or is there yet another possibility I have not considered?
Your container class shoudl have an attribute of Base *d instead of a
void*.
And, yes, return a B*
It would probably be worth mentioning that in the derived classes of
'C' (if any), you could even return a D1* or a D2* or..., since it
would be a *covariant* type.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
On Aug 20, 12:59 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Brian Withun" <bhwit...@gmail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
I have a base class (B) and several derived classes (D1, D2, ...)
I have a container class (C) with an attribute (void *d) which is
intended to point to any of D1, D2,...
My question is, on my C::GetObject() accessor, what is the most
appropriate return type?
void* C::GetObject( void ) { return d; }
or
B* C::GetD( void ) { return (B*) d; }
Is there any galvanizing rationale as to why I'd choose one return
type over the other, or is there yet another possibility I have not
considered?
Your container class shoudl have an attribute of Base *d instead of a void*.
And, yes, return a B*
Thanks for the suggestion. I'm going to go this way because it has
that intangible measure of elegance, and it makes the purpose of the
code more evident when reading it for the first time. Upon reading
your response I had the experience of, "now why didn't *I* think of
that?"
Brian Herbert Withun This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: BCC |
last post by:
Hi,
This is kind of a followup to a previous question...
I have a container class that has a load of variables (~100).
For example, I have groupings like this:
class CContainer {...
|
by: LuCk |
last post by:
Can someone explain what these really are for example:
----------------------------------------------------------
void SetFrameRate(int iFrameRate) { m_iFrameDelay = 1000 / iFrameRate; };
...
|
by: John J |
last post by:
I've written the following code into a class to search for and display the
results of all races entered (The complete code is in a previous thread). I
wish to amend the code so as to display the...
|
by: David T. Ashley |
last post by:
Hi,
In my project, I typically declare and define variables in the .H
file, i.e.
DECMOD_MAIN UINT8 can_message_201_status_global
#ifdef MODULE_MAIN
= HAS_NEVER_BEEN_RECEIVED
#endif
;
|
by: Matt Kruse |
last post by:
http://www.JavascriptToolbox.com/bestpractices/
I started writing this up as a guide for some people who were looking for
general tips on how to do things the 'right way' with Javascript. Their...
|
by: Kent |
last post by:
Hi!
I want to store data (of enemys in a game) as a linked list, each node will
look something like the following:
struct node
{
double x,y; // x and y position coordinates
struct enemy...
|
by: 42 |
last post by:
Hi,
Stupid question:
I keep bumping into the desire to create classes and properties with the
same name and the current favored naming conventions aren't
automatically differentiating them......
|
by: Bill Borg |
last post by:
Hello,
I've got a simple shared property, e.g.
Public Class dbObject
Private Const m_ID As String = "ID"
Public Shared ReadOnly Property ID() As String
Get
Return m_ID
End Get
End Property
|
by: Memfis |
last post by:
What is the best practice for using boost::signal? Should the signal be
a public field? Should an accessor method be used? Should there be some
special connection methods for every signal, like the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
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...
|
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,...
| |