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
1 answer

Friendship of class in C++

Consider the following program: #include using namespace std; class B; class A { int a; public: A():a(0) { } void show(A& x, B& y); }; class B { private: int b; …
0
votes
2 answers

C++ handle class abstraction

I am trying to abstract the explicit creation and destruction of raw handles with the usage of classes. The actual handle is stored as a private class member (so that the user doesn't interact with the lower-level details), which is created on…
MaximV
  • 155
  • 11
0
votes
1 answer

my production code stopped compiling after moving to visual studio 2019

The following simple application demonstrates the compile error: My class declaration: MyClass.h #pragma once class MyClass { friend int MyCalc(); public: }; class definition: MyClass.cpp #include "stdafx.h" #include "MyClass.h" int MyCalc() { …
0
votes
4 answers

Using friend function correctly across multiple Classes in C++

I am trying use a friend function. The function should be a friend to all the classes that i have. But i get multiple errors some of which says incomplete type. The following are files that i have: main.cpp #include #include…
Jason
  • 36,170
  • 5
  • 26
  • 60
0
votes
0 answers

Break Circular Dependency of Two Classes that Depend on Each Other

a part of my program basically boils down to the code below which won't compile due to circular dependency. I know there are previous discussions about this topic, but none of the previous solutions (at least none that I can find) resolves my…
0
votes
1 answer

2 way friendship and between classes and it's solution as forward declaration

I wanted to know if there's any exsisting solution to the problem: class b; class a { int x; public: friend class b; a() { x = 5 } void print(b obj) { cout << x << endl; cout << obj.y << endl; } }; class b { int y; public: …
0
votes
1 answer

Can a virtual function be a friend of another class?

Everywhere in theory, it is written that "A virtual function can be declared as a friend of another class", but practically on implementing it, the compiler throws an error saying "virtual functions cannot be friends". Here is a C++ program to…
Vanshika
  • 1
  • 6
0
votes
2 answers

friend injection fails on all compilers except MSVC

Consider the below code. struct foo { friend foo create(foo) { return {}; } }; int main() { auto a = create(foo{}); return 0; } This code compiles perfectly on all compilers. But the next below code compiles only in MSVC. struct foo…
김선달
  • 1,485
  • 9
  • 23
0
votes
1 answer

how to update status in friends table from pending to accepted?

After a lot googling i acheived to send a request to add as friend, say user a sends a friend request to user b and user b logs in he will see the request from user a ... say only his name is displayed when the user clicks on that name a dialog box…
user760844
  • 17
  • 1
0
votes
2 answers

Copy constructor in car class

Instructions: Define a Person class with the following class variables string firstName, lastName and address. The default constructor should set them all to the empty string. It should have setters and getters for each variable. Create a Car class…
KSky
  • 1
  • 1
  • 2
0
votes
0 answers

Allow descendant to call overridden protected function on instance of another descendant

Suppose we have a base class Base with a public interface, and some protected methods that are used to support the public interface. Simplified example: class Base { protected: virtual int helper() const = 0; public: virtual…
Marco Merlini
  • 875
  • 7
  • 29
0
votes
0 answers

Lambdas as friends to class with restricted access to protected members only

Consider the following code base design pattern: template class Foo { public: Foo(T t) : class_member_{t} {} const T class_member() const { return class_member_;} Foo(const Foo& other ) { class_member_ =…
Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
0
votes
1 answer

Friend Class across the Namespace not working in Xcode Cpp (macos dynamic library)

classA.h: #pragma once #include "classC.h" using namespace Bname; namespace Aname { class A { friend class B; private: void Aclassmethod(); }; } classB.h: #pragma once namespace Bname { class B { …
fiaazmd
  • 13
  • 2
0
votes
0 answers

How to simulate 'friend' in C++ with int main()

I want to make a singleton class with no public methods. I have achieved this functionality with another class that is a friend of my Main (it has private methods access). But I want to make sure of the same functionality but in my main function.…
0
votes
1 answer

How do I declare this templated class function as a friend?

Templates are making things so hard. I haven't got the syntax right, I've been trying for ages. I have a function called "createSphere" in a class called "ModelMngr" that I would like to be a friend of another class. class…
Zebrafish
  • 11,682
  • 3
  • 43
  • 119