Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Consider following program:
#include <iostream>
struct Test
{
int a;
Test(int s) : a(s)
{ }; // Observe this semicolon
int geta()
{
return a;
}
};
int main()
{
Test t(3);
std::cout<<t.geta()<<'\n';
}
The program compiles fine even when I use -pedantic-errors option in both gcc & clang. (See live demo here & here.) I also don't get any error from compiler if I put semicolon at the end of geta() member function like following:
int geta()
{
return a;
}; // This also compiles fine without any error or warnings in both g++ & clang
So, what is the purpose of allowing this unnecessary semicolon? Is there any use of this? Is it allowed explicitly by language standard?
Semicolon (;) stands for empty declaration in c++. You can see Declarations
C++ class allows following things inside the braces:
list of access specifiers
member object
member function declarations and definitions
Reference:Class
Thus going by above rule, empty declarations are allowed in c++. Which is why you have semicolon.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
As you might know,
A::A() {
this->foo = 1;
}
Is the same as:
A::A() : foo(1) {
this->foo = 1;
}
Which is inefficient because of the double declaration.
The compiler might optimize this, but in my case the class is not a POD.
I must define the member in the constructor body since it can't be compressed into one single line.
Is there any way of doing this?
No, you cannot initialise in the constructor body. It must be done in the mem-initialiser list, or using in-class initialisers (at member declaration). However, nothing prevents you from calling a function (or a lambda) to do the initialisation:
A::A() : foo([]() { /* ... */ } ())
{}
// or
A::A() : foo(initFoo())
{}
Foo A::initFoo() { /* ... */ }
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I new to programming. In stack overflow i couldn't see difference between : & :: is mentioned. Could anyone can explain in detail it helps to beginner learners like me. Thank you.
So you would use :: when you're defining/using methods from a class, so like for example
class foo{
public:
int bar;
int hi(int x);
int func(); // static member function
Foo(int num): bar(num) {}; // use of a colon, initialization list
};
int foo::hi(int x){
//define the function
}
Also if you have static member functions, you can just call those whenever through using foo::func(). You can find more about static member functions online.
The single colon is for member initialization list (you can look this topic up online) where you can initialization member variables in the construction of your class.
You can also find single colon used in polymorphism, when you derive a class from a base class. You can find more information about c++ polymorphism online.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to Interpret the name of class in following piece of C++ code ?
Following code is part of a project which is compiling successfully with g++ compiler.
class ABC::DEF
{
public :
int a;
void func();
};
void ABC::DEF::func() { a = 3; }
ABC::ABC() : OBJ(new DEF())
{
}
ABC::~ABC()
{
delete OBJ;
}
How to interpret ABC,DEF and OBJ in the above code?
How the constructor defined above works ?
The definition of ABC most likely looks something like this:
class ABC
{
public:
ABC();
~ABC();
private:
class DEF;
DEF* OBJ;
};
and what you're looking at is the definition of the class ABC::DEF and the constructors of ABC.
(This is a quite normal way of implementing the "pimpl idiom".)
ABC::DEF is a nested class. If you look at the definition for class ABC, you should see a forward declaration of class DEF. You can give the full definition for such a class outside of the outer class as shown in your question.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Imagine that you have a situation like this:
class className{
...
}
className func(){
className cl;
...
return cl;
}
int main(){
...
func();
}
What does the function func() return when you call it in the body of the program? A temporary copy of the object cl?
I don't understand this, since in the body of the function func() you can get the address &cl, but you get an error if you try to call &(func()) inside the function main().
Inside the function you are dealing with a so-called lvalue shortly speaking with an object which address is known because the object is defined explicitly.
The return value of the function is a temporary object (it is a so-called rvalue). Its address is not known. We do not know
where the compiler defined this object. So we may not apply operator & to a temporary object.
Another similar example
struct A
{
int x;
};
A f() { return A(); }
int main()
{
f().x = 10; // here the compiler will issue an error
}
This code shall not be compiled though for example MS VC++ 2010 will compile it due to either a bug or its language extension.:)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Is it bad to call global functions from member functions of a class? I mean is this ok...
class MyClass
{
void print_numb();
};
int get_numb()
{
return 10;
}
void MyClass::print_numb()
{
cout << get_numb() << endl;
}
If get_numb() is only used by a source file implementing MyClass then I'd put it in an anonymous namespace in that source file:
namespace /*no name here means the namespace is anonymous*/ {
int get_numb()
{
return 10;
}
}
That hides it away. I prefer that to a static private function in the class since it reduces the amount of stuff in the class declaration.
It is absolutely OK from the technical point of view: free-standing functions, global and static, are part of the language. There is no reason not to use them.
It is also OK stylistically: the Standard C++ Library provides free-standing functions, so the designers of the language were definitely OK with the idea of mixing member and non-member functions.
Of course you should take advantage of the C++ features that let you isolate your functions from functions in the libraries to which you link - by reducing their visibility to a single translation unit, or by placing them in a namespace.