• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
AussieSpoon

Can you return a class member object as a pointer?

19 posts in this topic

This may sound stupid (I'm pretty sure you can't) but I'm wondering if I have: 

 

class a {}

class b
{
public: 
    
    a GetA(){return MyObject;}

private:

    a MyObject;
};

 

Instead of GetA() returning MyObject as an object is it possible to return it as a pointer(*)?

 

Thanks

0

Share this post


Link to post
Share on other sites

If you want to return a reference to the class member, I suppose the reason is that you want to modify the member. But if the reason is that the object is big, and you want to improve performance by not having to return a copy, it may be a good idea to return a const pointer to the object, or const reference.

 

Returning a pointer (const or not) is valid C++, but it violates some principles of object oriented programming. If you want the object updated, the usual principle to have the update logic in a member function instead.

 

To expose internal design of a class to the outside is usually a bad idea. That means you have an extra dependency, and dependencies should be kept to a minimum. Suppose you later find out that the data need to be managed in another way, you can no longer update only the class 'b'. You will also have to find all users of GetA(), and update them. If it is a library, it may not be possible, and the API is suddenly incompatible.

0

Share this post


Link to post
Share on other sites
Maybe I'm nitpicky, but isn't it a bit wrong (and slightly confusing) to say that you "return an object as a pointer"?

Too me, that doesn't really make sense.
What you do is "returning a pointer that points to the object".

The pointer itself is an object too, a very different object then the class object it (maybe) points to.
I think that remembering this makes the answer more obvious.

This is an excellent point. And, while OT, another important one: people will talk about pass-by-value/pass-by-reference. in C/C++, everything is pass-by-value; it's just that sometimes, you pass values that are pointers, so the objects they represent don't copy.

-2

Share this post


Link to post
Share on other sites

 

in C/C++, everything is pass-by-value...

 

 

Please identify the value is being passed in the following code:

 

void frobnicate(int &example) {
    example = 42;
}

I suppose he would've been correct if he said, "In C, everything is passed by value...."

 

But, not so for C++, as you point out.

0

Share this post


Link to post
Share on other sites
I suppose he would've been correct if he said, "In C, everything is passed by value...."
While this is what I meant to say, I'm still not technically wrong.
 
Please identify the value is being passed in the following code:
void frobnicate(int &example) {
    example = 42;
}


When the compiler creates a function like this (and assuming it doesn't inline it), it needs to be able to change the value of that variable within the function. The C++ specification deliberately says that the actual way this happens is implementation defined, and that whether a reference actually uses storage or not is unspecified.

In some implementations, this is just a pointer, which means that yes, the value of the pointer is passed onto the stack. In this case, the "reference" semantics C++ gives you are nothing more than syntactic sugar with extra compile-time checking. Under the hood, it's nothing more than your standard raw pointer.

Sometimes you can't get around doing that unless you want to write better algorithms. Some compilers don't bother. Mine doesn't.
Edited by Geometrian
-1

Share this post


Link to post
Share on other sites
When the compiler creates a function like this (and assuming it doesn't inline it), it needs to be able to change the value of that variable within the function. The C++ specification deliberately says that the actual way this happens is implementation defined, and that whether a reference actually uses storage or not is unspecified.


In some implementations, this is just a pointer, which means that yes, the value of the pointer is passed onto the stack. In this case, the "reference" semantics C++ gives you are nothing more than syntactic sugar with extra compile-time checking. Under the hood, it's nothing more than your standard raw pointer.

Sometimes you can't get around doing that unless you want to write better algorithms. Some compilers don't bother. Mine doesn't.

Don't confuse language concepts and implementation details. The fact that it is a pointer "under the hood" is, as you say, an implementation detail, but it is not a pointer being passed by value as far as the language is concerned; it is passed by reference.

 

If you bring implementation details and "under the hood" into the picture, then nothing exists in any language, because it all boils down to machine code being executed in one way or another, and machine code typically has no templates, no structures, no functions, no scope, no name spaces, no closures, no typing... nothing. A language just abstract such things so that the programmer can write code what the program is supposed to do, and the compiler/virtual machine/whatever ensures that the program is actually doing just that on the particular computer running it.

2

Share this post


Link to post
Share on other sites
Don't confuse language concepts and implementation details. The fact that it is a pointer "under the hood" is, as you say, an implementation detail, but it is not a pointer being passed by value as far as the language is concerned; it is passed by reference.

Actually, my bringing in of implementation details was to cement that references may in fact be pointers--for the sake of showing that my argument holds in actual architectural-level practice.

As far as your distinction, I think (in this case) it's a distinction without a difference. I will agree that references and pointers have a few semantic differences (for example, you can do arithmetic on pointers, and (so) references can do more checking), but really there's no difference between them. They act the same way, and you can use one instead of the other with almost equal facility.

 

For purposes of this discussion, the key point is that passing references into functions works in the same way as passing pointers. In the above example, the answer is "the reference int&example is passed by value, since its value is copied"--nevermind that that copying might be optimized away, just as inlining might similarly be able to optimize away other copies for pointers. Semantically thinking of the reference being copied doesn't break anything, and is actually closer to truth in practice.

 

If you bring implementation details and "under the hood" into the picture, then nothing exists in any language, because it all boils down to machine code being executed in one way or another, and machine code typically has no templates, no structures, no functions, no scope, no name spaces, no closures, no typing... nothing. A language just abstract such things so that the programmer can write code what the program is supposed to do, and the compiler/virtual machine/whatever ensures that the program is actually doing just that on the particular computer running it.

Reducto ad absurdum, but still somewhat relevant. Machine code has comparably few features, I'll agree, but in this case, the idea of "pointer"s actually exists, albeit not with static checks.

And I completely agree that whether "references" are implemented at the machine level through pointers or at the compiler level through optimizations, the point is that they exist in C++, which abstracts it. However, the converse of that was never my point; as above, I brought in implementation details to show how it works in practice. I had intended the first main paragraph of my last post to address that.

The point with all this is that the (key) properties of "references" exactly mirror those of pointers: they are datatypes held in variables, and they get copied by value when they are passed into functions, just as much as pointers. That's the functionality that C++ exposes and abstracts for us. C++ programmers might not think about it that way, since in the ideal case, they can be optimized just like any other code. But as we've agreed, that last is an implementation detail.

Edited by Geometrian
-2

Share this post


Link to post
Share on other sites

I knew assembly language (Z80 and x86) before I learned C, and it was trivial to think of a natural assembly implementation of most things in C. When I was learning C++, I wouldn't consider that I understood a feature unless I had some idea of how that would be implemented in assembly. In particular, thinking of references as memory addresses with slightly different syntax than pointers was very helpful to me.

 

On a side note, I still don't think I truly understand exceptions, in that sense: I should probably spend a couple of hours figuring it out.

0

Share this post


Link to post
Share on other sites

[quote name='Paradigm Shifter' timestamp='1357693013' post='5019295']You can sort of do exceptions in C with setjmp/longjmp. That's how SSE is done in Windows.[/quote]*SEH wink.png 

 

[quote name='Álvaro' timestamp='1357692811' post='5019293']On a side note, I still don't think I truly understand exceptions, in that sense: I should probably spend a couple of hours figuring it out.[/quote]They key part of C++ exceptions (as opposed to SEH/setjmp/longjmp type ones) is that stack-unwinding takes place. I learnt a lot about this by writing a stack-allocator that supports unwinding (calling destructors of things in the stack in the opposite order to creation).

0

Share this post


Link to post
Share on other sites
Yeah, I meant SEH, but I have had several cans of Lidl's finest cheapest ales ;)<br /><br />Stack unwinding is the big difference, yeah.
0

Share this post


Link to post
Share on other sites
The point with all this is that the (key) properties of "references" exactly mirror those of pointers: they are datatypes held in variables, and they get copied by value when they are passed into functions, just as much as pointers. That's the functionality that C++ exposes and abstracts for us. C++ programmers might not think about it that way, since in the ideal case, they can be optimized just like any other code. But as we've agreed, that last is an implementation detail.
 

No, C++ programmers don't think about it that way because in terms of the semantics of C++ (not the implementation details), references are not values. We are discussing the semantics of C++ here. The implementation details are interesting and a C++ programmer will often find it useful to keep them in mind, but as far as I can tell, they are not relevant to the discussion of the semantics of C++, the programming language. Edited by Oberon_Command
0

Share this post


Link to post
Share on other sites
If you want to claim that implementation details are what counts, many C++ compilers don't have pass by value for objects. When you call a function that uses pass by value they create a new object in the caller's frame and pass by address to the function. In those compilers only primitives and pointers have pass by value. And then you have some compilers where some objects are pass by value and some others aren't, depending on size, whether or not they contain floating point variables or even more obscure criteria.
2

Share this post


Link to post
Share on other sites
Even pointers are a high-level language concept. At the level of implementation detail, there are no such things as pointers.
There are typically registers with values in them, and instructions that operate on those registers, some of which fetch values from places in memory relating to the value a register held.

Regardless of whatever merrit your argument has on some level, it simply isn't helpful to make the point you are trying to make, because it contradicts the description of the language itself.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0