Questions tagged [friend]

In object-oriented programming, friend refers to a method or class that has access to some non-public aspects of a particular class.

In object-oriented programming, friend refers to a method or class that has access to some non-public aspects of a particular class.

Different programming languages use the friend keyword differently. Make sure it is clear which language you are asking about.

1518 questions
0
votes
0 answers

Template friend function of template class that returns a reference to an implementation class object

I have this template function and class in mc.h/mc.cpp files: template class MC; template MemT & getMCObj(int t_instNum); class HBM; template class MC { public: MC(){} friend MemT &…
rishi_v
  • 1
  • 2
0
votes
0 answers

Friend functions, Data managing, OOP, Cpp

A curious question about the process of Friend Functions on Cpp. Can we use friend functions as setters and getters (Manipulators, Accessors), or in other words, what is the difference between them, knowing that setters and getters are only for a…
rekkalmd
  • 171
  • 1
  • 12
0
votes
1 answer

How to avoid access errors while using friend functions in c++?

#include using namespace std; class boss{ int salary; public: boss(); boss(int b){ salary = b; } friend void total(boss, employe); }; class employe{ int salary; friend void total(boss, employe); public: employe(); …
0
votes
1 answer

How to declare a friend function of a template inside a class and implement this friend function ouside class?

Well, I'm trying to implement the copy_and_swap idiom on my first Stack in C++, for that I need to create a swap function and this swap function needs to be a friend function, I tried to do it by this way: template class Stack { …
FUNKYBAT
  • 71
  • 1
  • 1
  • 8
0
votes
1 answer

allocating and deallocating dynamic memory seg fault in c++

The main objective of this project is to implement my knowledge of dynamic memory allocation and deallocation. So far, my program compiles and runs, but the only problem I am having is that the compiler states Segmentation fault (core dumped) at the…
0
votes
0 answers

Error with friend non-member function which gives a segmentation fault

For this project we are suppose to cover the topic of dynamic memory allocation and deallocation. The functionality of the 'operator<<; is to output(to terminal or file depending on the type of ostream& os object passed as a parameter to it) the…
0
votes
1 answer

declaring a friend template function

This question is based on partially on exercise 15-4 in Accelerated C++ [1] and the code in that book. I want to declare the myclone function as a friend of a class so that it has access to private members. The myclone function is declared as a…
0
votes
1 answer

Does this code cause a violation of the one-definition rule?

I'm trying to work out under what circumstances the following code might cause a violation of the one-definition rule. header.h #pragma once #include template class C {  friend std::size_t f() {   // Uses the…
0
votes
0 answers

Overloading the comparsion Operator for Objects

I'm trying to figure out if it is possible to overload the comparison operator for a member of an object. I know it is possible to overload the comparison of two objects, but what about the members specifically? class Foo() { private: int a; …
0
votes
1 answer

C++ operator overloading with "friend" keyword

In our C++ programming class, professor is using "friend" keyword to overload operators. However, when I search on the internet, most people don't use "friend" keyword. So, do we need to use "friend" keyword for operators? Is there such a rule or…
0
votes
0 answers

Explain in simplest way(maybe some illustrations) why should/shouldn't I use friend function?

I still don't quite understand the concept of OOP in C++. Is using friend function a bad practice? Does it reduce encapsulation? Why should I use friend function instead of changing my private members to public members? I found that friend function…
la lala
  • 41
  • 2
  • 5
0
votes
1 answer

Member function inside templated operator overload not working

My printTree function won't work in the overloaded ostream operator. Error and code below. Error code : C3861 ('printTree': identifier was not found) Description: 'printTree': function was not declared in the template definition context and can be…
0
votes
1 answer

friend operator<< of a template class, with additional default template parameter

In the following code, A is a template class, depending on a non-type bool type parameter. A friend operator<< is defined for both A and A. The operator<< additionally depends on another bool template parameter. #include…
francesco
  • 7,189
  • 7
  • 22
  • 49
0
votes
3 answers

Why am I getting the error 'cannot access private member declared in class' even though I have declared friend class

Consider the following code: #include template class A { private: T value; public: A(T v){ value = v;} friend class A; }; template class B { public: T…
James
  • 398
  • 1
  • 6
  • 19
0
votes
1 answer

Friend methods as Constant

Why can't I declare a friend function as const? //Types.h #pragma once #include #include class Player { public: //constructors Player(); Player(const std::string&, unsigned short); //operator overload friend…
Simo Pelle
  • 141
  • 1
  • 11