• 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
serratemplar

C++ Inheritance confusion

11 posts in this topic

This has got to be a simple mistake, but I've stared at it for too long now and I just don't see it. sad.png (Excuse the canned example; it's pretty close to the real thing though.)
 

class Foo {

public:
Foo(vector<string> things, string place) : m_thingList(things)
{
log("in Foo ctor");
validatePlacePriorToAssignment(place);
}

virtual ~Foo() {}

private:

vector<string> m_thingList;
string m_validatedPlace;

validatePlacePriorToAssignment(string p)
{
// do stuff to validate place; if not valid, throw exception
// else
m_validatedPlace = p;
}
...
};

class Bar : public Foo {
public:
const string defaultPlace = "Validville";
Bar(vector<string> stuff) : Foo(stuff, defaultPlace)
{
log("in Bar ctor");
}
...
};

someFunctionSomewhere(vector<string> allTheThings) {
try { Bar barbar(allTheThings); }
catch (...) { log("saddness"); }
}

Okay, here's the weirdness I'm seeing: there is a log entry for the Foo ctor but not the Bar ctor. I'm stack-allocating a member of a derived class; my understanding is that the base ctor is called THEN the derived ctor is called...but in this case that doesn't seem to be happening!

 

I'm worried that I'm doing inheritance wrong, or using initializer lists wrong, or I'm  using them wrong in the context of inheritance, or for some reason it's wrong (not allowed) to validate prior to assigning in a constructor. I think I'm doing this all correctly, but obviously I'm not so here I am. smile.png

I've read through a bunch of tutorials and stackoverflow examples this afternoon, and - for the life of me - I can't figure out why this isn't working; it looks like all the sample code. If you can catch it, I'd be especially grateful.

 

Thank you in advance. <3

1

Share this post


Link to post
Share on other sites

Removing the init lists entirely results in the kind of behavior I'd expect, i.e. I see log entries from both ctors and in the order I'd expect (Foo then Bar).

0

Share this post


Link to post
Share on other sites

It looks correct. Is the exception being triggered? Maybe "validatePlacePriorToAssignment(place);" is crashing and you aren't realizing it.

2

Share this post


Link to post
Share on other sites

That's got to be it! I commented out the validate() call and just set the member in the init list. So...good. Thank you for being a sanity check for me, haha. :)

A little odd; I thought for sure I was catching there, but I am demonstrably incorrect. I was looking in the very wrong place.

 

Thank you!

2

Share this post


Link to post
Share on other sites

That (const string bug) was actually a second bug, well shielded by the first one. :)

I fixed it with stackoverflow: http://stackoverflow.com/questions/1563897/c-static-constant-string-class-member

 

In short, it's now declared (static const) in the class decl, then defined in the cpp file, but still scoped to the class (which is what I wanted).

 

Second set of eyes for the win. :) Nice to see it running again.

0

Share this post


Link to post
Share on other sites

This is the reason I usually stick with trivial, or even empty, constructors, and then call an explicit init() function after instantiation.  Constructos, exceptions, and manual memory management are a really bad combination :-) .  The problem is even worse for destructors.

0

Share this post


Link to post
Share on other sites

That's true. Another reason for an init()-type function is to reuse the memory of same class instance rather than deleting and then allocating a new object, or to intentionally delay some heavy operation until a more convenient time.

 

I meant that avoiding constructors and destructors because they can cause bugs if misused doesn't mean your code will be safe - you'll just be looking at a different set of bugs. Constructors and destructors go hand in hand with memory management. 

 

Using an init() function when it is part of the class's desired interface makes sense. Using an init() function solely because you don't like constructors doesn't make sense. Doubly sole for destructors.

0

Share this post


Link to post
Share on other sites

So, while we're on the topic, what are the direct thoughts on my design there? I.E. using a validator (and potentially throwing) from the constructor (the latter an idea I got from Scott Meyer, iirc) vs. constructing empty then compelling the user to fill in the blanks prior to calling run().

 

The reason I wanted to have the constructor do the validation and bomb gracefully upon a fail is that I want the interface here to be very difficult to use incorrectly, and there's (at least) two parts to that, which I see: fewer calls need to be made (less internals exposed) and it still prevents you from doing something bonkers (in my example there, you can't do any object-supported operations on your list of things if the place isn't valid).

 

think that, were I to allow empty construction then push init responsibility to the user, there's not much of a difference: init() could throw instead of the constructor, and you'd still have an object in an empty/usable state. (That has some appeal.) I'm honestly not clear on what the right answer to this is.

0

Share this post


Link to post
Share on other sites

RAII says to initialize your data, and to this I agree.  

 

It is in contrast to other languages (such as C) where a structure can be created but initially contains unspecified garbage, just whatever happened to be in memory at the time. Empty is initialized, random unspecified garbage is uninitialized.

 

People who come from a more modern language background (C#/Java/Python) are used to members being automatically initialized for you, so when they see RAII they interpret it differently than it was meant. Back in the day when the alternative meant having unspecified values, including invalid values that looked like valid values, RAII meant creating an allocation function that allocated memory and then immediately called something like bzero() or memset() or otherwise immediately assigning valid values if zero is not appropriate, then returned the initialized resource.

 

 

 

 

In my experience it is fine to allow a convenience non-default constructor if you want, but beware of error conditions.

 

It is generally best to have a default constructor that only creates and empty object. This way when someone allocates an array or temporaries or other quick-to-do things, they immediately get back the objects without any fuss.

 

If you want to provide an additional constructor that builds up an object based on additional data you can do so but it adds complexity. Now you have a constructor that is likely to have error conditions (so you may need to throw based on all possible error conditions, in turn forcing you to catch all the exceptions that might be thrown at construction time). Such a constructor takes more time and blocks execution. Such a constructor generally makes some things a little more complicated in exchange for convenience in another area. Yes, for some things there is no additional cost and the difference is that the values are assigned to only once rather than twice; for other objects, however, the cost to the application can be quite large. When such a constructor requires trips to disk, trips to the network, or any other long-running or blocking behavior, I generally question its necessity.

Edited by frob
2

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