Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


A little bit of clarification.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Crusable   Members   -  Reputation: 594

Like
0Likes
Like

Posted 22 December 2012 - 01:20 AM

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, 22 December 2012 - 01:21 AM.


Sponsor:

#2 L. Spiro   Crossbones+   -  Reputation: 13958

Like
0Likes
Like

Posted 22 December 2012 - 03:11 AM

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, 22 December 2012 - 03:15 AM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#3 haegarr   Crossbones+   -  Reputation: 4421

Like
0Likes
Like

Posted 22 December 2012 - 04:35 AM

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.

"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.

 

 

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

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.



#4 Khatharr   Crossbones+   -  Reputation: 3030

Like
0Likes
Like

Posted 22 December 2012 - 04:40 AM

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.


void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS