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

How do I access member variables from one class into other using friend functions in C++?

I am using fried functions for the very time and was assigned to complete an incomplete code using friend functions as below. //CODE GIVEN IN THE QUESTION NOT MEANT TO BE EDITED #include using namespace std; class store_keeper; class…
ba11b0y
  • 161
  • 3
  • 18
0
votes
2 answers

Not able to Instantiate class inside friend function? I am getting not declared in scope error

I am not able to instantiate test class inside the friend function, the compiler throwing error ptr not declared in this scope. I believe friend functions have access to all the private and public members of the class yet I am getting this error. I…
Sadik Khan
  • 29
  • 3
0
votes
1 answer

How can I friend a derived class function in the base class?

I have the following code: #include class A { private: int a; public: void setA(int a_); friend int B::getA(); }; class B : public A { public: int getA(); }; void A::setA(int a_) { a =…
Collin
  • 1,777
  • 4
  • 26
  • 42
0
votes
1 answer

template friend operator new mingw32

My understanding is that operator new cannot be templated (when declared using the parameters I have used.. (size_t) The following compiles OK on mingw32 and arduino DUE, but not on other platforms e.g. linux. Is there a bug in mingw32, or should…
0
votes
4 answers

Friend function and Inheritance in cpp

Am new to cpp. Generally, friend function could access private and protected members of the class but when I tried to use friend function in derived class am observing weired behavior.. class first { int a; }; class second : public first { …
0
votes
1 answer

wxThread documentation example usefull or bad coding practice?

I have a C++ wxWidgets program, that uses threads. Therefore I needed to make sure the threads are safely accessed. In the wxThread documentation it is explained how to do so. They use friendclasses and wxCriticalSection to secure their threads. I…
Lehue
  • 425
  • 8
  • 26
0
votes
1 answer

Difference in behavior while using friend function and normal public function

Why do I get different results for the following two cases? I am passing by value in both cases. Case1: using public member function: (output: 2:1) #include using namespace std; class one { int hrs; int mins; public: void in(int h,…
Saideep
  • 61
  • 1
  • 6
0
votes
1 answer

What is the preferred way to override operators in C++

I was always overriding operators like this: class MyClass { public: ... MyClass operator+(const MyClass&) const; private: int some_number = 5; }   MyClass MyClass::operator+(const MyClass& rhs) const { return MyClass(some_number +…
Not Szwagi
  • 81
  • 2
  • 10
0
votes
3 answers

Some compiler errors concerning an overloaded operator on a template in c++

I have some code with a few errorr I do not understand how to fix at all. I have asked my professor and TA, and consulted the internet with no luck, apart from understanding more precisely what the errors mean. From what I can tell, the compiler…
joedillian
  • 177
  • 3
  • 15
0
votes
1 answer

PHP+Ajax : Add-a-friend system

I'm trying to make a little project for adding friends. When you click the add friend button you are sending an Ajax call with the friend id & username (they are the id and name attributes of each add button) to the add.php file. (The mysql…
user484957
  • 121
  • 1
  • 6
0
votes
0 answers

Friend functions not being able to access private members

My ostream friend function works correctly but my ofstream nor ifstream work correctly as the errors that are given state that the different private members cannot be accessed through the ofstream or ifstream functions. Please help. class Module {…
0
votes
2 answers

Declare many functions as friends of a class

How to conveniently declare many template functions as friend function of a template class? Example: template void funct1(MyClass & A); //1.forward declaration. template class MyClass{ protected: T a; friend…
rxu
  • 1,369
  • 1
  • 11
  • 29
0
votes
0 answers

C++ restrictive template friend function in template class

Consider the code below: // no forward declarations // // template // class A; // // template // void swap(A& lhs, A& rhs); template class A { int m_i; public: // template // friend void…
Kemal
  • 849
  • 5
  • 21
0
votes
2 answers

Accessing Protected Attribute of Template Class

The following code doesn't work because the t member function can't access the attribute of its argument object. How to declare template method t of template class A as a friend function of A? For the code without template, there is no need to…
rxu
  • 1,369
  • 1
  • 11
  • 29
0
votes
1 answer

Superclass Setting Object of Subclass Data

I have a base class in C++ that has some protected member variables (although, I do not think it is relevant that it is protected vs. private in this case). I have a derived class that derives from this base class. In it, there is a public function…
T Lytle
  • 177
  • 2
  • 13