1) what exactly is the difference in a constructor if you put reference on it?

example

ConstructorX(string first):firstName(first)

with

ConstructorX(string &first):firstName(first)

2)

const Array &operator=(const Array&)

the first const what exactly means?the const in the parenthesis is that the object is constant if its at the end means the function cannot modify values,but when its on the start?what that means?

ConstructorX(string first):firstName(first)

basically, when you pass an argument to this function, your program will:
1)create an string object(first)
2)copy contents from the argument passed to the object
3)firstName will be initialized with the object(note: another copy activity)

now, if you use this way

ConstructorX(string &first):firstName(first)

your program will:
1)pass the argument directly to the function(skipping the overhead of creating and then copying another object)
2)firstName will be initialized with the object(note: another copy activity)

note that here, it passes the argument directly so it gives you the possibility to change it's value/contents in the function; for ex

#include <iostream>
void AddOne(int x)
{
    // x is a local variable
    x++; // increments the copy witch won't help us at all
    // x goes out of scope, the argument passed remains unchanged
}
void AddOne2(int &x)
{
    x++; // x is a reference to the argument, so, the argument increments
}

int main( )
{
    int nVar = 1;
    AddOne(nVar); // nVar value remains 1
    std::cout << nVar << std::endl; // prints 1;

    AddOne2(nVar); // nVar value increments to 2
    std::cout << nVar << std::endl; // prints 2;

    return 0;
}

if you still want your function to get the argument directly excluding the possibility to be changed just declare it as a constant reference

void AddOne2(const int &x)
{
    x++; // error, x is constant
}

so, your function will be faster if you use const references

const Array &operator=(const Array&)

means that the operator= returns a constant reference to an Array object, this makes almost no difference to 'Array &operator=(const Array&)', the only thing you'll be restricted to do by the 'const' in front of the function is this:

Array a, b, c;
(a = b) = c;// you won't be allowed to do this
// witch is redundant and confusing
// this will output an error because (a = b) will return an constant object and the next statement tries to assign a value to it (const a = c) witch is wrong
a = b = c; // note that you will still be allowed to do this
// witch is the exact same thing as a = c; b = c;

Edited 5 Years Ago by dospy: n/a

sorry for bothering but i have another question,
i do an exercise with inheritance and i cant find the error

Package.h constructor

Package(const string&,const string&,const string&,const string&,int=0,
            const string&,const string&,const string&,const string&,int=0,
            double=0.0,double=0.0);

Package.cpp

Package::Package(const string &sendName,const string &sendAddress,const string &sendCity,
        const string &sendCountry,int sendCode,const string &recieveName,
        const string &recieveAddress,const string &recieveCity,
        string &recieveCountry,int recieveCode,double w,double cost)
: senderName(sendName),
  senderAddress(sendAddress),
  senderCity(sendCity),
  senderCountry(sendCountry),
  senderZipCode(sendCode),
  recipientName(recieveName),
  recipientAddress(recieveAddress),
  recipientCity(recieveCity),
  recipientCountry(recieveCountry),
  recipientZipCode(recieveCode)
 {
    setWeight(w);
    setCost(cost);
}

TwoDayPackage.h

class TwoDayPackage : public Package {
public:
    TwoDayPackage(const string&,const string&,const string&,const string&,int=0,
                  const string&,const string&,const string&,const string&,int=0,
                  double=0.0,double=0.0,double=0.0);

TwoDayPackage.cpp

TwoDayPackage::TwoDayPackage(const string &sendName,const string &sendAddress,
        const string &sendCity,const string &sendCountry,int sendCode,
        const string &recieveName,const string &recieveAddress,
        const string &recieveCity,string &recieveCountry,int recieveCode,
        double w,double cost,double flatF)
: Package(sendName,sendAddress,sendCity,sendCountry,sendCode,recieveName,
        recieveAddress,recieveCity,recieveCountry,recieveCode,w,cost),
        flatFee(flatF){
}

the error that i recieve is that :

TwoDayPackage.cpp:8: error: prototype for `TwoDayPackage::TwoDayPackage(const std::string&, const std::string&, const std::string&, const std::string&, int, const std::string&, const std::string&, const std::string&, std::string&, int, double, double, double)' does not match any in class `TwoDayPackage'
TwoDayPackage.h:7: error: candidates are: TwoDayPackage::TwoDayPackage(const TwoDayPackage&)

i can see that the error is at twodaypackage class but where???i counted variables,consts,commas,references everything seems to be ok...

i counted variables,consts,commas,references everything seems to be ok...

I see a missing const in your definition, but I think a bigger issue is that you're passing way too many parameters. If you find yourself passing more than three, there might be something wrong with your design.

well i noticed that is a big chaotic,i will have it in mind,how i could do it?to have less?smaller classes?

thanks i thought i had counted all the consts :D

i hate thoose small mistakes that a detail can affect and you have to search where you forgot a mark or letter.

now i have this error :

TwoDayPackage.cpp:6: error: default argument missing for parameter 6 of `TwoDayPackage::TwoDayPackage(const std::string&, const std::string&, const std::string&, const std::string&, int, const std::string&, const std::string&, const std::string&, const std::string&, int, double, double, double)

i have to pass 12 variables and i am passing 12.

default argument missing for parameter 6

Defaulted parameters cannot be followed by non-defaulted parameters because the compiler has no way of determining when you want the argument defaulted and when you're simply calling the function incorrectly:

foo(int x, int y = 0, int z); // Illegal, y is defaulted and z is not
foo(int x, int z, int y = 0); // OK, all defaulted parameters are at the end

as Narue stated, you have to put all the default parameters at the end of the function declaration
as this

TwoDayPackage(const string&,const string&,const string&,const string&,
              const string&,const string&,const string&,const string&,
              double=0.0,double=0.0,double=0.0,int=0,int=0);

and for the too many arguments problem, you could use vectors..
ex:

TwoDayPackage(const vector<string>& strVector, const vector<double>& doubleVector, const vector<int>& intVector);

and you could use a define to trick the compiler to let you call it with one argument(treat rest like default)

#define TDPackage(x) TwoDayPackage(x, vector<double>( ), vector<int>( ))

edit: i've seen that the string parameters are related to each other, in this case you could create a special storage class and make it the function parameter like this

class StrStorage
{
public:
string m_Data1;
string m_Data2;
// ... etc
StrStorage(const string& nData1, const string& nData2) : m_Data1(nData1), m_Data2(nData2) { }
~StrStorage( );
}

// and than call the function this way
StrStorage strings("string 1", "string 2");
TDPackage(strings); // note you will have to modify the function to accept the StrStorage instead of a vector<string>
// or alternatively
TDPackage(StrStorage("string1", "string2"));// i won't use this one because you have to pass a lot of strings and that's waht you are trying to avoid

Edited 5 Years Ago by dospy: n/a

This question has already been answered. Start a new discussion instead.