Itt miért hívódik meg kétszer az operator ushort? C++.
A kimenetben látszik(mert ki iratom),hogy ez a kódsor lefutásakor(*Kokszos = *Bolyhos + *Morcos;) meghívódik kétszer az operator ushort függvény ezt nem értem.Miért?
Ha lehetne még egy kérdésem akkor mondjátok meg,hogy miért dob hibát ha ++(*Morcos) a morcos itt nem teszem zárójelbe?
..........................................................
//main.cpp
# include <iostream> //std::cout,std::cin,std::endl
# include <string> //std::string
# include <windows.h> //Sleep(1000),exit(0)
# include <stdlib.h> //EXIT_SUCCESS
typedef unsigned short ushort; //szinoníma készítése az unsigned short-ra.
typedef unsigned int uint; //szinoníma készítése az unsigned int-re.
using std::cout; //cout esetén mindig az std:: névteret használjuk.
using std::cin; //cin esetén mindig az std:: névteret használjuk.
using std::endl; //endl esetén mindig az std:: névteret használjuk.
using std::string; //string esetén mindig az std:: névteret használjuk.
//CAT osztály
class CAT
{
public:
CAT(); //Konstruktor1
CAT(ushort age,ushort weight); //Konverziós konstruktor2
CAT(const CAT &rhs); //Copy konstruktor
~CAT(); //Destruktor
const CAT operator=(const CAT &rhs); //operator=
const CAT& operator++(); //prefix
const CAT operator++(int); //postfix
const CAT operator+(const CAT &rhs); //operator+
operator ushort()const; //konverziós operátor
void SetAge(ushort age); //SetAge
ushort GetAge()const; //GetAge
void SetWeight(ushort weight); //SetWeight
ushort GetWeight()const; //GetWeight
private:
ushort *itsAge;
ushort *itsWeight;
};
//CAT osztály definíció
//Konstruktor1,Konstruktor2,Copy konstruktor,destruktor,operator=
CAT::CAT():
itsAge(new ushort(0)),
itsWeight(new ushort(0))
{cout <<"Konstruktor1 \n";}
CAT::CAT(ushort age,ushort weight = 0):
itsAge(new ushort(age)),
itsWeight(new ushort(weight))
{cout <<"Konstruktor2 \n";}
CAT::CAT(const CAT &rhs)
{
cout <<"Copy konstruktor \n";
itsAge = new ushort;
itsWeight = new ushort;
*itsAge = rhs.GetAge();
*itsWeight = rhs.GetWeight();
}
CAT::~CAT()
{
cout <<"Destruktor \n";
delete itsAge;
delete itsWeight;
itsAge = NULL;
itsWeight = NULL;
}
const CAT CAT::operator=(const CAT &rhs)
{
cout <<"operator= \n";
if(this==&rhs)
return *this;
delete itsAge;
delete itsWeight;
itsAge = new ushort;
itsWeight = new ushort;
*itsAge = rhs.GetAge();
*itsWeight = rhs.GetWeight();
return *this;
}
//operator++ pre,operator++ post,operator+,operator ushort
const CAT& CAT::operator++()
{
cout <<"operator++ prefix \n";
++*itsAge;
return *this;
}
const CAT CAT::operator++(int)
{
cout <<"operator++ postfix \n";
++*itsAge;
return *this;
}
const CAT CAT::operator+(const CAT &rhs)
{
cout <<"operator+ \n";
return CAT(*itsAge+rhs.GetAge(),*itsWeight+rhs.GetWeight());
}
CAT::operator ushort()const
{
cout <<"operator ushort \n";
return(ushort(*itsAge));
}
void CAT::SetAge(ushort age)
{*itsAge = age;}
ushort CAT::GetAge()const
{return *itsAge;}
void CAT::SetWeight(ushort weight)
{*itsWeight = weight;}
ushort CAT::GetWeight()const
{return *itsWeight;}
//main start
int main()
{
ushort age = 200;
const CAT *const Bolyhos = new CAT(4,7);
CAT *const Morcos = new CAT(2,3);
CAT *Kokszos = new CAT;
const CAT &Bolyhos2 = *Bolyhos;
cout <<"\nAGE: \n"
<<"Bolyhos most "<< Bolyhos->GetAge() <<" eves \n"
<<"Bolyhos2 most "<< Bolyhos2.GetAge() <<" eves \n"
<<"Morcos most "<< Morcos->GetAge() <<" eves \n"
<<"Kokszos most "<< Kokszos->GetAge() <<" eves \n\n"
<<"WEIGHT: \n"
<<"Bolyhos most "<< Bolyhos->GetWeight() <<" kg \n"
<<"Bolyhos2 most "<< Bolyhos2.GetWeight() <<" kg \n"
<<"Morcos most "<< Morcos->GetWeight() <<" kg \n"
<<"Kokszos most "<< Kokszos->GetWeight() <<" kgs \n\n";
cout <<"CALL *Kokszos = *Bolyhos + *Morcos: \n";
*Kokszos = *Bolyhos + *Morcos;
cout <<"\nCALL age = Bolyhos2: \n";
age = Bolyhos2;
cout <<"\nCALL *Morcos = age: \n";
*Morcos = age;
cout <<"\nCALL ++(*Morcos): \n";
++(*Morcos);
cout <<"\nCALL (*Morcos)++: \n";
(*Morcos)++;
cout <<"\nAGE: \n"
<<"Bolyhos most "<< Bolyhos->GetAge() <<" eves \n"
<<"Bolyhos2 most "<< Bolyhos2.GetAge() <<" eves \n"
<<"Morcos most "<< Morcos->GetAge() <<" eves \n"
<<"Kokszos most "<< Kokszos->GetAge() <<" eves \n\n"
<<"WEIGHT: \n"
<<"Bolyhos most "<< Bolyhos->GetWeight() <<" kg \n"
<<"Bolyhos2 most "<< Bolyhos2.GetWeight() <<" kg \n"
<<"Morcos most "<< Morcos->GetWeight() <<" kg \n"
<<"Kokszos most "<< Kokszos->GetWeight() <<" kg \n\n";
delete Bolyhos;
delete Morcos;
delete Kokszos;
return EXIT_SUCCESS;
}
*Kokszos = *Bolyhos + *Morcos;
Itt szerinted hány macskát konvertálsz ushorttá? Össze tudsz adni macskát és ushortot? Nem. Tehát mindkettőt konvertálni kell.
++(*Morcos)
Nézd meg az operátorok precedenciáját. Vagy mire van itt hiba?
Kapcsolódó kérdések:
Minden jog fenntartva © 2025, www.gyakorikerdesek.hu
GYIK | Szabályzat | Jogi nyilatkozat | Adatvédelem | Cookie beállítások | WebMinute Kft. | Facebook | Kapcsolat: info(kukac)gyakorikerdesek.hu
Ha kifogással szeretne élni valamely tartalommal kapcsolatban, kérjük jelezze e-mailes elérhetőségünkön!