answersLogoWhite

0

Is there any friend constructor in c?

Updated: 8/21/2019
User Avatar

Wiki User

10y ago

Best Answer

C is not an object-oriented programming language so there is no friend keyword let alone friend constructors.

In C++, however, constructors can be declared friends. Consider the following code where the class Y default constructor has private access to X::foo() because the Y::Y() constructor is explicitly declared a friend of class X.

Note that Y must be defined before X can be defined, thus X must be forward declared.

#include<iostream>

class X; // fwd declaration

class Y

{

public:

Y() { X x; x.foo(); } // X::foo is private, but Y::Y() is a friend.

};

class X

{

friend Y::Y(); // friend constructor

private:

void foo() {}

};

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Is there any friend constructor in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is friend constructor?

A friend constructor is a constructor that is declared a friend of another class and that grants that constructor private access to the class in which it is declared a friend. Example: class Y { friend char* X::foo (int); // friend function friend X::X (char); // constructors can be friends friend X::~X(); // destructors can be friends }; For more information, see '11.3 Friends' in the current ISO C++ Standard.


What is the difference between constructor and friend function in c plus plus?

A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.


Constructor and destructor invocation in c?

Not possible in C.


What is the difference between implicit and explicit call of constructor in c plus plus?

An implicit constructor call will always call the default constructor, whereas explicit constructor calls allow to chose the best constructor and passing of arguments into the constructor.


True or False A C plus plus class constructor cannot return a function value?

True - A C++ constructor cannot return a value.


What is conustrutor?

A constructor is a function in C which has the same name of the class. The constructor can be used to initialize some function.


What is a default parameterized constructor?

There is no such thing as a default parameterized constructor. The default constructor is always the 'no-arg' constructor and does not take any parameters or arguments as input


Is Constructor Can be Private and Protective in c?

question i understand not


What are the reasons for rise of constructor concept in c?

Not sure what you mean by this. C is a not an object-oriented programming (OOP) language, and therefore has no constructor concept. You probably meant C++ but, even so, there is no "rise of constructor concept". Constructors are fundamental to OOP -- they allow you to initialise an object at the point of instantiation.


What is the use of private constructor in c plus plus?

Private construction prevents objects from the class from being instantiated other than via a static member function of the class, a friend function or a friend class.


What happens when no constructor function is declared in a class - in Java?

When any constructor is deffined in your class, the java compiler create a default no argument constructor for you. This constructor only have an invocation to the super class constructor (" super( ) ").


How is the default constructor equivalent to a constructor having default arguments?

Any constructor that can be invoked without explicitly passing any arguments is a default constructor. Note that there can be only one default constructor so there can only be one constructor where all arguments have default values or one constructor that has no arguments, but not both. A constructor where all arguments have default values is a useful means of combining two or more constructors into a single default constructor, thus reducing verbosity and code duplication.