tags:

views:

485

answers:

9

I have a couple questions.

Are all functions inside of a class member functions? or only the ones preceded by the declaration "friend"?

The significance of member functions are that they cannot be accessed by any other classes correct?

What is the difference between an implicit and explicit call?

Which functions can or cannot be implicitly called?

I was hoping to see an example of implicit and explicit calling.

EDIT: Thanks for the great answers, there were lot of bits and pieces that answered my question and thanks for the links to the books. I will read them.

+6  A: 

Read Thinking in C++ by Bruce Eckel. It's an excellent, very readable e-book freely downloadable, and will answer all your questions.

slashmais
+2  A: 

I think a good start for you will be to read this site: The C++ FAQ.

However, to give you a brief answer: All functions and variables declared within a class definition are members of that class. The "friend" keyword has a special meaning relating to who can access the various members.

Access rights are controlled by whether the member has been declared in the public, private or protected sections of the class definition.

Reading the C++ FAQ should give you a good idea of how these pieces fit together.

dominic hamon
A: 

Not sure what you mean by implicit and explicit...

Class members are private by default. Adding a friend specification allows another class to access private members. For example:

class A
{
   void hidden();
   friend class B;
};

class B
{
   void callIntoA( A& a )
   {
       a.hidden();
   }
};

Or, if you want to make something public you could specify a "public:" section as follows:

class A
{
    void hidden();

public:
    void notHidden();
    void alsoNotHidden();

private:
    void alsoHidden();
};
Philip Davis
+1  A: 

Are all functions inside of a class member functions? or only the ones preceded by the declaration "friend"?

Friend functions are not member functions. All what they differ from regular global functions is that they can access non-public area of the class. For example:

class myclass
{
    friend void fun(const myclass& obj);
    int x;
};

void fun(const myclass& obj)
{
    std::cout << obj.x; // x is private member
}


What is the difference between an implicit and explicit call?

When you call a function using the () operator, it is an explicit call. If you don't do it that way, it is an implicit one. Example of an explicit call:

fun();

Examples of implicit calls:

void someScope(){
    myclass myobject; // constructors called

} // destructor of myobject is called before exiting the function
....
myclass* mySecondObject = new myclass; // constructor called
delete mySecondObject; // destructor called
AraK
+3  A: 

Constructors, destructors, and operator type() functions are called implicitly.

rlbond
A: 

Are all functions inside of a class member functions? It depends what you mean by "inside". You can define free-standing (non-member) functions within the class namespace. Functions declared as "friend" are not member functions (if they were, they wouldn't need to be declared as friends). Non-friend functions in the class definition are member functions.

Member functions with "public" access are accessible by other classes and free-standing functions.

Implicit function calls often arise in the context of type conversions and object construction. Here's an example :

class BigBuffer
{
public:
 BigBuffer(int initialValue)
   { memset(buffer, initialValue, sizeof(buffer)); }
private:
 char buffer[65536];
};

extern void Foo(const BigBuffer& o);

void oops()
{
 Foo(3);
}

Foo(3) results in an implicit call to the BigBuffer constructor to convert the integer 3 into a BigBuffer object the Foo function is expecting.

Jim Lewis
A: 

To answer the questions:

All functions in the class declaration are member functions, except the ones marked "friend". Those are other classes or functions that can access the internals of the class in question.

Member functions are functions that do something relevant to the class. They are private by default, or if they come after a private: designator, meaning they can only be accessed by other functions in the class. Other access modifiers are protected, meaning they can be accessed by other class functions and in functions in classes that inherit from this class, and public, meaning they're fair game for everybody to use. Classes or functions that are specified as friend can access private and protected data and function members.

I don't know what you mean by implicit and explicit. One possible meaning has to do with the explicit keyword, which can be applied to constructors.

In class Foo, suppose I declare

Foo(int i);
explicit Foo(const char * s);

That means that, if I mention an int value somewhere, the compiler can treat it as if it were a Foo, but in order to treat a string as a value I'd have to be explicit about it, so if I have

f(Foo f);

declared somewhere, then

f(1);
f(Foo("foo"));

are legitimate calls, but

f("foo");

isn't.

From the nature of your questions, I'd say you need a good elementary C++ book. There's a StackOverflow question about excellent C++ books that I'd recommend, in addition to the C++ FAQ Lite. You can learn by studying programs and asking random questions here, but it will take longer and you'll miss things.

David Thornley
+2  A: 

There is a subtle difference between implicit and explicit calling functions. Consider the following test-case out of the Standard doc

struct A { };
void operator + (A, A);

struct B {
  void operator + (B);
  void f ();
};

A a;
void B::f() {
  operator+ (a,a); // ERROR – global operator hidden by member
  a + a; // OK – calls global operator+
}

The first one fails because you gave a function name explicitly, and the name will be looked up "inside out": first in the function, then in its class, and then at global scope.

In the second one, lookup for function candidates works different: Member and non-member functions are looked up in two phases, and when looking up Non-member functions, member functions are ignored, so the member operator won't hide the global operator in the second case.

Explicit function calls will also never select a built-in operator. You cannot do operator+(10, 12); for example. In general, always prefer implicit function calls, i would say.


Are all functions inside of a class member functions? Or only the ones preceded by the declaration "friend"?

As someone else said too, friend functions are not members of the class that contains the declaration, even if the functions is defined in the declaration too

struct A {
    friend void f() {
      std::cout << "member of the global namespace" << std::endl;
    }

    void g() {
      std::cout << "member of class A" << std::endl;
    }
};

The lexical scope of f is A - that means you can refer to (static) members or nested types of A without preceding their names with A::.


With regard to special member functions, you can call a destructor or assignment operator explicitly, while constructors cannot be called explicitly. Their calls are implicit, and is part of creating a new object.

Johannes Schaub - litb
A: 

Explicit means that the function will only be called if you specifically call it in your source code.

Mostly this is used with constructors to keep the compiler from using a specific constructor when creating temporaries or doing conversions.

For example:

class FooA
{
public:
   FooA();
};

class FooB
{
public:
   FooB();
   explicit FooB(const FooA &other);
};

Now lets say you have this function:

void MyFunc(FooB &var) { ... }

and this code

void main()
{
   FooA bar;
   MyFunc(bar);

   FooB foo(bar);
}

Normally, the compiler would be able to construct a FooA type from a FooB type through the constructor if you didn't have the "explicit" keyword. Now it'll complain that it doesn't know how to make a FooA from a fooB when you pass it to MyFunc().

It will not complain, however, about the construction of foo from bar because you explicitly called it.