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.
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 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 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.
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 8 years ago.
Improve this question
In C++13/4 environment, what's the difference between a namespace and a class?
The way I see it;
namespace foo
{
int a : 4;
int b : 4;
}
and
class bar
{
public:
int a = 0;
int b = 0;
}
is the same thing...
Yes, they're accessed different as such;
namespace....
foo::a=20;
foo::b=30;
class....
bar alpha;
alpha.a ...
alpha.b ...
But in general, what's the advantage of one over the other?
There are plenty of differences. A namespace is a grouping mechanism for names, nothing more or less. On the other hand, classes:
Are types.
Can be instantiated.
Can be derived from.
Can have public, protected, and private members.
Can have virtual functions.
And so on.
If you find yourself wondering whether you should use a namespace or a class, then you are probably just looking for a way to control the scope of names---so the choice is clear: use a namespace.
Namespaces allow you to group entities into having a local scope rather than global scope. This is common with the standard library, std, for instance.
An example group of names may be streams such as cout and cin.
Without using a namespace, you have to define the namespace scope std.
std::cout << "Hello, world!";
With a namespace, you have revealed the scope once and no longer need to declare it again.
using namespace std;
cout << "Hello, world!";
However, in the latter case, you could not use another variable named cout that is user-defined or contained within another library.
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 8 years ago.
Improve this question
I am working on OOPs and using C++. I have one class accessing object of other class/struct.
struct data
{
int a;
int b;
string str;
} sd;
class format
{
int x;
void show()
{
cout << data.a << endl;
}
};
which one is best to use here class or struct?
First of all, it's struct, not strut.
Second, you cannot access member a like you do, data.a, but rather sd.a, because you need to access it on an instance, not on the name of the struct.
For the detailed differences between class and struct see this SO question and its two best rated answers.
I use this convention:
A struct only have members that it make sense to manipulate directly
A class may have complicated rules for assigning members
This somewhat fits well with the default accessibility rules. But as said before in this thread, the choice depends on convention.
that depends on your requirement the only difference in struct and class is in struct all members are public by default and private in case of class
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
My question is simple, what is the performance loss due to reference length. I cannot explain myself but here is the sample:
between this
C* pC = m_a->m_b->m_c;
and this expression
C* pC = m_b->m_c;
I am asking this because I have a global class which has a Singleton pattern and holds everything. I am accessing all of its members from its members like this.
class Global
{
A* a;
X* x;
};
class A { B* b; };
class B { C* c; }; // etc
class X { Y* y; };
class Y { Z* z; };
class Z
{
void foo() { Global::GetInstance()->a->b->c->foo(); }
}
Is this a good design? Any advice for this? I am having some trouble with this topic too Qt Architecture Advice Needed
Every -> operator is an indexed indirection, which costs a cycle or two, depending on the processor, and may be invisible if its pipeline is good enough.
However the real question here is 'compared to what?' What other implementation techniques are you considering for solving this problem? Unless you have a viable alternative your question is really meaningless.
Similarly the frequently-asked question about the relative efficiency of virtual and non-virtual functions is meaningless unless it takes into account how to get the same effect both ways. In the non-virtual case this amounts at least to an 'if' or 'switch', whose cost has to be added in to the comparison.