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

A little bit of clarification.

3 posts in this topic

Hello, I was re-reading my c++ book, to see if I could better understand some stuff, but i am left with more questions.  I am reading Beginning programming c++ through game programming second edition, and i have the following questions:

 

1. friend functions: i'm just overall confused on how I write/implement them and how to use them. 

 

2. Overloading operators: First, let me see if i am understanding this. You need to overload operators when you are using them with your own classes. So do i need to rewrite the function for for each class i use them with. Also, how do i write the function and what parameters do i use.

 

3. Copy constructor: what purpose do they serve and how to i write the/ implement them.

 

Also, if i explained anything wrong in this please correct me. If you have any websites, videos, or other information i can use to better understand these concepts, please tell me. Thank you for all replies.

Edited by Mathew Bergen
0

Share this post


Link to post
Share on other sites
Actually I will soon be late for a party so I really have time only to quickly type out one of the answers you seek. If I return from the party and no one else has answered your remaining questions I will then answer them.
1. friend functions: i'm just overall confused on how I write/implement them and how to use them. 

I am a class. I want some parts of me to be accessible by anyone and some parts to have restricted access (not going to go into the differences between protected and private here since it does not matter).
Usually I do this by declaring things (functions or members) I want everyone to access with “public :”.

Sometimes though I have secrets that I want to hide except from my best friends, because I trust them to never tell anyone where the body is buried and I love to brag (it may be a secret, but I have to tell someone).
In that case I declare them as friends. The concept is as explained—they have access to all of your secret things that you have hidden from everyone else.
The implementation is as follows:
class CRenderTargetInterface {
    friend class CDirectX9RenderTargetManager;
Inside the class who wants to reveal his or her secrets to his or her friend, declare that friend as a “friend”.
In other words CDirectX9RenderTargetManager can access anything inside of CRenderTargetInterface via this code. CDirectX9RenderTargetManager knows where CRenderTargetInterface buried the bodies.


L. Spiro Edited by L. Spiro
0

Share this post


Link to post
Share on other sites

[quote name='Mathew Bergen' timestamp='1356160830' post='5013351']
2. Overloading operators: First, let me see if i am understanding this. You need to overload operators when you are using them with your own classes. So do i need to rewrite the function for for each class i use them with. Also, how do i write the function and what parameters do i use.
[/quote]

"Using" is too broad. You need to overload operators when your class represents a data type for which the operator may be applied. If your class represents a complex number, than you may find it convenient to provide an addition and multiplication operator to allow for mathematical syntax programming style, for example. When your class represents a frequency analyzer than it "uses" the complex numbers, but itself need not define operators for them.

 

The exact syntax and the belonging parameters of operator overloading depend on the specific kind of operator. If I've counted correctly, then there are 46 operators that can be overloaded. A quick googling bring e.g. this page up: Wikibooks/C++Programming/Operators/Operator_Overloading.

 

 

[quote name='Mathew Bergen' timestamp='1356160830' post='5013351']
3. Copy constructor: what purpose do they serve and how to i write the/ implement them.
[/quote]

Copy constructors are initializers not from scratch but from a specimen. E.g. it copies the data of one object into the data of another object of the same type. You can declare copy constructors itself, but also the compiler will generate one if, well, "needed to do so". Whether the copy is 100% or else only in parts is up to you (i.e. the programmer). It is useful in some situations. E.g. initialization from a library where a prototype / specimen / archetype / prefab / ... whatever-you-call-it resides, or the amount of singular constructor parameters would be unhandy, or you want to rely on the correctness of parameters without processing them again (they *were* already processed when being set in the specimen object, aren't they?), ...

 

For the various syntaxes, again a quick googling brings up e.g. this wikipedia article.

0

Share this post


Link to post
Share on other sites
2. Overloading operators: First, let me see if i am understanding this. You need to overload operators when you are using them with your own classes. So do i need to rewrite the function for for each class i use them with. Also, how do i write the function and what parameters do i use.


No, sah. You can overload operators, but there's no case when you really need to unless you're fiddling with something (some api or library) that demands a class with a specific operator overload.
 

class Example {
public:
  Example(int a, int b, int c, int d) {
    ary[0] = a;
    ary[1] = b;
    ary[2] = c;
    ary[3] = d;
  }

  int getNumberAtIndex(size_t index) {
    return ary[index];
  }

  int addToAllNumbers(int valueToAdd) {
    for(int i = 0; i < sizeof(ary); ++i) {
      ary[i] += valueToAdd;
    }
  }
private:
  int ary[4];
};

Example ex(1, 2, 3, 4);
ex.addToAllNumbers(7);
int theThirdAryVal = ex.getNumberAtIndex(2);

You know how that works, yes?
 
You can call 'getNumberAtIndex()' to grab a value from the array. Well:

class Example {
public:
  Example(int a, int b, int c, int d) {
    ary[0] = a;
    ary[1] = b;
    ary[2] = c;
    ary[3] = d;
  }

  int operator[](size_t index) {
    return ary[index];
  }

  //do not do this (see notes after code)
  int operator+(int valueToAdd) {
    for(int i = 0; i < sizeof(ary); ++i) {
      ary[i] += valueToAdd;
    }
  }

private:
  int ary[4];
};

Example ex(1, 2, 3, 4);
ex + 7;
int theThirdAryVal = ex[2];

That's just another way of doing the same thing. A word of advice - only use operator overloads when the result of invoking the overload function would be obvious to someone else using your class, even if they don't read your documentation. Sometimes people do some really outrageous stuff with operator overloads and can create some really awesome bugs. Operator overloading is more or less a way of providing some 'syntactic sugar' for your class. Too much sugar rots your teeth. There's a running gag on this forum about overloading the comma operator. A class with an overloaded comma would call that function whenever you try to use it in an array or a list of arguments!
 

3. Copy constructor: what purpose do they serve and how to i write the/ implement them.


The copy constructor is used to replicate an object. If you don't specify one (or disable it by prototyping it but not implementing it) then the compiler will create one for you on the sly. Example:

 

class Thingy {
public:
  //This is a constructor (explicit prevents a single-arg ctor from becoming a copy ctor)
  explicit Thingy(int val) {
    myval = val;
  }

  //This is a copy constructor.
  Thingy(Thingy& other) {
    myval = other.myval;
    cout << "Copied!" << endl;
  }
private:
 int myval;
};

Thingy a(7);
Thingy b = a; //will print "Copied!"
//the 'myval' of b is 7

You should disable the copy ctor if you don't want to use it by declaring it in the private section and then not defining it.

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