CLASSES


Copy constructors, default constructors, assignment operators

player player1; //default constructor
player1=player2; //assignment operator
player player1=player2; //copy constructor

temp objects are in stack objects in memory are in heap


this->vec used for referring to objects in the class. this is a pointer

const int* GetPos() const {return vec;}
constant functions do not change any of the member variables, otherwise there will be a compile time error

Explicit vs silent conversions

class player{
  private:
  char vec;
  int vec2;
}

player player1(5,10) <-- automatically puts it in WITHOUT explicitly definiting the function


We use classes because it acts as a black box, and encapsulates the methods that we use.

Hard to use incorrectly because the interface is well-defined and explained.

Structs are used when you want to keep things public rather than somewhat private with classes.

 Str::Str() : mLen(0), mPtr(new char[mLen+1]) { ... } 

is equivalent to

Str::Str() {
  mLen=0;
  mPtr=new char[mLen+1];
  *mPtr='\0';
}

student(std::string const &n)
{
  name=n;
}

^ name is created, then name is initialized then assigned

student(std::string const &n):name(n) {...}

^ THIS IS BETTER. USE IT!!!! Note: The variables are initialized in the order they're listed in the private scope.


Str (const Str &string)

^Copy constructor

PROBLEM WITH COPY CONSTRUCTORS: Pointers get copied over. This is bad because there will be two class objects with member variables pointing to the same location. If the destructor gets called, you can guess what would happen.

THEREFORE, always write your own copy constructors if you are placing stuff in memory.


class Str
{
public:
  void print(){ std::cout<<mPtr<<std::endl;} //this is actually void print_Str(Str *ptr_to_Str_object) {std::cout<<ptr_to_Str_object->mPtr<<std::endl;}
private:
  char *mPtr;
}

' Only member functions can access member variables. Therefore, there is no security problem!

void print() const // void print_str(str const* this)

if you use new className() then you HAVE to call delete or the destructor will NOT GET CALLED


If you have no constructors, one will be made for you. However, it most likely won't do what you want.


s2=s3; //s2.operator=(s3) which is also operator=(&s2,s3)


Rule of three: Destructor, copy constructor and assignment operator


Str& operator=(Str const&)

Assignment operator


Do not return a reference to an automatic object. Automatic objects are temporary objects.


Try to always pass in by reference, and if possible, with const as well.


To iterate through an object, it must have begin and end members that return iterators.

What this means is that there should be begin() and end() functions that return the appropriate iterators.


typedef char * iterator

is the same as

using iterator = char*;


// begin(Str *this) iterator begin() { return mPtr; }

THUS won't work with constant strings.


If Name(Str const &s) then Str can be passed in instead of Name. Same way that Str(const char *s) would be able to be passed in as "TEST"

THIS DOES NOT WORK RECURSIVELY.

For example, passing in "TEST" to Name does NOT work!


explicit keyword ( put in front of functions ) makes it so that you cannot explicitly convert a certain argument to the required parameter. Only used for single argument constructors.


friends can touch your privates. friends are bad. If you want to be a bad person, implement friends like this:

friend void whatever(whatever);

or

friend class Rectangle;


const_cast, static_cast, etc


Putting the copy constructor and assignment operator in private makes it so that you cannot access it from outside of the class. BUT you can access it from the member functions within the class!

BUT in C++11

you can do this! HomeForSale(HomeForSale const &) = delete;