-
June 17th, 2018, 05:14 AM
#1
friend function
My target is to define friend function to exchange 2 value inside class but I have problem to compile code, suggestions? What mistake did I make?
Code:
class Coppia {
unsigned long int pos;
unsigned long int neg;
Coppia(const Coppia&); //mask copy constructor
friend void scambia(Coppia&);
public:
Coppia() { pos=5; neg=9; };
Coppia(unsigned long int p, unsigned long int n) { pos=p; neg=n; };
friend ostream& operator<<(ostream&, const Coppia&);
};
class Insieme {
int ncop;
Coppia* ins;
Insieme(const Insieme&); //mask copy constructor
public:
Insieme(int);
friend ostream& operator<<(ostream&, const Insieme&);
Insieme& operator=(const Insieme&);
Insieme& operator-();
~Insieme();
};
#include <iostream>
#include "compito.h"
using namespace std;
ostream& operator<<(ostream& os, const Coppia& c)
{
os << "<" << c.pos << ",-" << c.neg << ">";
return os;
}
void scambia(Coppia& c)
{
unsigned long int tmp=0;
tmp=c.pos;
c.pos=c.neg;
c.neg=tmp;
}
Insieme::Insieme(int n)
{
if (n>0) {
ncop=n;
ins=new Coppia[n];
for(int i=0; i<n; i++) {
Coppia ins[i];
}
}
}
ostream& operator<<(ostream& os, const Insieme& i)
{
for(int k=0; k<i.ncop; k++)
os << i.ins[k];
return os;
}
Insieme& Insieme::operator=(const Insieme& i)
{
if (this!=&i) {
if (ncop!=i.ncop)
delete[] ins;
ncop=i.ncop;
ins=new Coppia[ncop];
for(int k=0; k<ncop; k++)
ins[k]=i.ins[k];
return *this;
}
}
Insieme& Insieme::operator-()
{
for(int i=0; i<ncop; i++)
scambia(ins[i]);
return *this;
}
Insieme::~Insieme()
{
if (ins!=NULL)
delete[] ins;
}
I thought to define friend function scambia, which can access to private members of Coppia class, and I'd like to invoke scambia inside operator-.
What do you think?
There is another thing which I'm not able to understand.
When s'It necessary to define friend function inside private or public section of class?
-
June 17th, 2018, 05:26 AM
#2
Re: friend function
Why can't it be a member function that takes no arguments? It also, looks like you can just use std::swap to implement the function.
I note that your Insieme constructor implementation looks wrong: I'm not sure what you're trying to achieve with the for loop, but why don't you just have a std::vector<Coppia> member object?
-
June 17th, 2018, 06:52 AM
#3
Re: friend function
1) The #includes etc need to be before the class definition as the class uses these
2) Coppia ins[i]; ??? This is definitely wrong. What are you trying to achieve?
Correcting 1) and 2), the code compiles.
You have tried to implement operator= and there is no copy constructor!!! You need to implement a copy constructor for a class that uses dynamic memory if you are going to allow copy, assignment etc.
In the destructor, why are you testing ins against NULL? Firstly, this should be nullptr and not NULL, secondly ins is never set to nullptr?? and thirdly if ins is nullptr then you don't need the test as delete ignores nullptr.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 17th, 2018, 11:11 AM
#4
Re: friend function
Originally Posted by 2kaud
1) The #includes etc need to be before the class definition as the class uses these
OK I removed it before to paste code, but I can add it again.
Code:
#include <iostream>
using namespace std;
2) Coppia ins[i]; ??? This is definitely wrong. What are you trying to achieve?
I know, infact compiler gave me error. Inside operator- function, I'd like to invoke function/method which can access to private member of Coppia class but I didn't understand hot to make it.
You have tried to implement operator= and there is no copy constructor!!! You need to implement a copy constructor for a class that uses dynamic memory if you are going to allow copy, assignment etc.
II forgot to write that main.cpp invokes no copy constructor so I mask it avoiding its use inside main.cpp
In the destructor, why are you testing ins against NULL? Firstly, this should be nullptr and not NULL, secondly ins is never set to nullptr?? and thirdly if ins is nullptr then you don't need the test as delete ignores nullptr.
you are right
-
June 17th, 2018, 12:08 PM
#5
Re: friend function
If you write a class that uses dynamic memory and you are going to allow copy, assignment etc then you need to write a copy constructor and a swap function. operator= is then implemented in terms of these. Whether your main() at the moment uses a copy constructor directly is neither here nor there. The compiler can call it when it needs to - and without an explicit correct copy constructor that does a deep copy, the default does a shallow copy that is most certainly not what you want when dynamic memory is involved. Your operator=() is as used for c++98, not how it's done today in c++.
For a class that uses dynamic memory, the first things you write, test and make sure are working are
- default constructor
- other constructors as needed
- destructor
(- some way of viewing the class data)
- copy constructor
- swap function
- copy operator=
- move constructor
- move operator=
Then you start to add other functions, operators etc as needed.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 19th, 2018, 02:10 PM
#6
Re: friend function
When s'It necessary to define friend function inside private or public section of class?
Friend declarations are not affected by access modifiers such as protected and private.
That is, you can put a friend declaration wherever you want in the class definition.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|