473,787 Members | 2,938 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best return type of an accessor to void*

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

Aug 20 '07 #1
5 1563
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::GetObje ct( 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; }
Aug 20 '07 #2
"Brian Withun" <bh******@gmail .comwrote in message
news:11******** **************@ 22g2000hsm.goog legroups.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*
Aug 20 '07 #3
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<typena me T>
class C
{
private:
//Some type of data structures to store multiple instances of T.
std::list<T*int ernal_list; //example
std::vector<T*i nternal_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.GetObjec t();

C<NotDerivedFro mBthird;
NotDerivedFromB * ndfb = third.GetObject ();

Aug 20 '07 #4
Jim Langston wrote:
"Brian Withun" <bh******@gmail .comwrote in message
news:11******** **************@ 22g2000hsm.goog legroups.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
Aug 20 '07 #5
On Aug 20, 12:59 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
"Brian Withun" <bhwit...@gmail .comwrote in message

news:11******** **************@ 22g2000hsm.goog legroups.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
Aug 20 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2235
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 { m_circle_area; m_circle_position;
3
4551
by: LuCk | last post by:
Can someone explain what these really are for example: ---------------------------------------------------------- void SetFrameRate(int iFrameRate) { m_iFrameDelay = 1000 / iFrameRate; }; ---------------------------------------------------------- I know what functions are and i know what void is and stuff but i dont get the whole `Accessor Methods` term i guess.
15
2237
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 best result only. Can anyone suggest a simple amendment to the following that will result in only the best result being displayed?
6
677
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 ;
136
9459
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 code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
10
15134
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 *enemydata; // Holds information about an enemy (in a game) // Its a double linked list node
14
3140
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... (both are "Pascal Case" with no leading or trailing qualifiers). For example... I'll be modelling something, e.g. a computer, and I'll
4
1586
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
3
2690
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 following: private: signal<void()x; public: void connectX(const signal<void()>::slot_type& slot); void disconnectX(const signal<void()>::slot_type& slot);
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10172
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.