in working on this pgm, i have made notes to myself ... questions that keep popping up. if anyone wants to clarify some of these concepts for me ....

1. are TRUE and FALSE keywords? if a method says
return (TRUE);
will it work?

2. const on END of method declaration and const IN the parameter of the method declaration ...
sometype somemethod (const sometype& x) const
what do both of these do? are they basically INSURANCE ... just be sure the method won't change the parameter passed in and ... ?

3. does the deconstructor happen at the end of the pgm automatically. i know that you need to manually delete things sometimes ... like in an assignment ... but does the
virtual ~ClassName(); deconstructor somehow automatically called on all objects that have been created at the end of method main?

4. scenario 1
int* d_array;
d_array = NULL;
this is read as d_array points to an int and the object that d_array points to is NULL (which would be 0 in the case of an int) ... is that right?
scenario 2
int* d_array;
*d_array = NULL;
what does this say? would it cause an error? is the '*' only used when declaring a pointer?

5. what is the proper way to do the & and * ...
int *d_array; or
int* d_array;

6. i am confused with the usage of "this" and "self" in C++.
could someone "read" both of these to me (i still have to make a sentence out of statements and methods to understand them and draw a mental picture).

C& C:: operator = (const C& rhs)
{
   if (*this == rhs)
       return *this;
...........
}

and then there is this one .....

C& C::operator = (const C& rhs)
{
    if (this == &rhs)
        return *this;
..............
}

and then there is this one .....

C& C:: operator = ( const C& rhs){
     if (this == &rhs)
       return (self);
}

i think they all do the same thing (check for assignment to itself) ... but i am unsure about the *this vs self and &rhs stuff ... so basicaly i am confused by the whole thing.

7. the use of "new". is that basically only done in a constructor?


THANKS SO MUCH ... you guys have already been so much help to me. i am on a crash course!

crq

1) No, and maybe.

2) Say that the parameter shouldn't be changed and say that the method won't change the state of an object.

3) Yes, but only for objects that aren't dynamically allocated. For dynamic allocation, the destructor is called when the memory is released.

4.1) Close enough.
4.2) It's an error because d_array doesn't point to anything predictable and when d_array is dereferenced, you get an int, not a pointer to int. NULL should only be used with pointers.

5) There's no difference. Use whichever you find easier to understand.

6) self doesn't exist in C++, this is a way to access an object from within one of its type's member functions.

7) No.

" if (*this == rhs)"

is testing the CONTENTS of the two objects (maybe using an operator); so they may not be the same actuall class, but may contain the same thing. For operator = that is not likely to be an issue (i.e. you won't copy the contents if they are already equivelent), but it might matter on other operators.

Also, comparing the contents of objects is almost always going to be slower than comparing the pointers. Consider if the objects are trees and the == operator traverses the tree!

About TRUE and FALSE; they are not built in to the language, though for example they are defined in headers like windows.h, so you see them used a lot in windows code. Note, though, that in windows, they are #defines:

#define TRUE 1
#define FALSE 0

and so they are integers and NOT of type 'bool'. This can cause you to get compiler warnings with such simple things as:

bool on;
on = TRUE; // may generate compiler warning because 1 is not 'true' or 'false'.

So, generally better to stick to using 'bool', 'true', and 'false' unless you are the victim of some external API, like Windows. In those cases, use TRUE and FALSE for the routines that Windows expects you to use them with.

this > the object refered to is the class you called the operator function on
*this > a pointer to "this" object

C& C operator=(const C& rhs)
{
    if(this == rhs) // if THIS object (the one the func belongs to) = the input object...
        return *this; // ...return a pointer to this object (as func returns C&)
}

example

C cclass1; // an object of the above class
C cclass2;

cclass1 = cclass2; (this = cclass1, *this = &cclass1, rhs = &cclass2)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.