Using : and :: in C++ without confusing [closed] - c++

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
Both : and :: punctuations in C++ can bind ownership of function within class, or subclass within base class. But it's not fully clear when to use each in general, and which to put in header and which in cpp source files (if this is a matter of personal flavor, please tell more common convention). Concise example would be appreciable so much!
Please correct me if I am wrong or not precise enough, and list other cases if I missed some for using : and ::
Thanks in advance.

bind is not the best word to use for this.
:: scopes functions and so on.
For example std::cout says there is something called cout in a scope (namespace, class...) called std.
: does not do this and can be used in a variety of ways.
It can follow an access specifier, for example public:.
It is also part of the ternary conditional x ? y : z

Related

Is it okay to shorthand a function call using a macro? C++ [closed]

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 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
I am slowly writing an emulator for a gameboy using C++, I am currently working on the CPU side of things. I have written a generic function that takes any two registers of the CPU and returns as a Word data type. As it is necessary to access individual registers and also a combination of registers.
const Word get_word(Byte *registerOne, Byte *registerTwo)
{
return ((*registerOne << 8) | *registerTwo);
};
calling this function gets tedious as you have to specify each register
get_word(&this->registers.h, &this->registers.l)
My question is if it okay to define a macro like so
#define get_HL() get_word(&this->registers.h, &this->registers.l)
since now I can call it using
get_HL()
The reason why I want to do it like this since I don't want to create more private/public functions that just perform function calls.
I have tried compiling and it seems to work as it should since its just a pre-processor macro but I am not sure of the design implication
EDIT:
Okay I mean there are glaring flaws with this and you should just make a function, just as much work to make a function or write a macro.
const Word get_HL() { return this->get_word(&this->h, &this->l); };
Let this be a post for people who had the same idea and hopefully stop making the same mistake
No, this isn't OK in my opinion. It hides what arguments you're passing into the function, and macros don't respect scopes and as such are highly susceptible to name conflicts. This seems like an ideal use case for a non-static member function.

Terminology for a class who has something as a member [closed]

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 3 years ago.
Improve this question
In object oriented programming terminology, I can simply say:
- Member: I mean to say a member of (this) class (which I'm referring to)
But I don't know what is the correct terminology for this:
- ? : I mean to say a class who has (this thing I'm referring to) as a member
Maybe I can use owner or parent. Any idea?
FWIW the C++ standard calls this "containing object" or "containing class object" in a couple of places. It never formally defines the term though.
I guess you can call the corresponding class "the containing class".
In good old days there was a term nesting used in a variety of ways( nesting scope, nesting namespace, nesting class, nesting function...). I don't know if it is still widely used, but IMHO it does the purpose.
Regards,
FM.
I ended up using this as the most clear:
... classes which own this class as member ...

C++ operator overloading for embedded DSL language in C++ [closed]

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
Is it possible to use C++ operator overloading and create a DSL-like syntax for embedded DsL code in c++.
"a": auto = call("add2Numbers", "b", "c");
This is what I would like ideally. But anything close to this as valid C++ would be acceptable.
Good advice: don't. C++ is a big multi purpose language and already complicated enough. You will confuse people (and yourself too!) if you randomly change stuff. Especially operator overloading and the proposed pre processor should be treated carefully.
My advice for you is to write the functions you need. You gave an example about some kind of assignment (sorry, don't understand the given code) and I'm sure it is perfectly possible to write a function with a convenient interface to achieve what you're trying to do. The advantage is that your synthax stays all C++ and possible readers (and again your future self) don't get confused.
I hope I understood your question properly. If not, please correct me.
Felix

Is there any way to restrict inheritance? [closed]

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
Please provide a simple solution to restrict inheritance in C++.
Yes! As of C++11, there's a final specifier you can use to indicate that a class cannot be inherited from:
class DontInheritMe final {
// This class cannot be inherited from.
};
If you have experience in Java, the final keyword in C++ in this case works the same was as final classes in Java.
If u really dont want to inherit a class,then just make its member variables and member functions as private ,if other class tries to inherit it they cant have access to its funtions and variables anyways.
But using final is a better option

C++ - Best Practice: `using std::cout` vs `std::cout` [closed]

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 5 years ago.
Improve this question
I understand that, in C++, we should never use:
using namespace std;
The two possible alternatives are:
1) adding using std::cout; at the beginning of the file and just type cout whenever required
2) type std::cout every time we need to use cout
My understanding is that the second method is the best. However, is this always followed in professional environments? Is it practical to follow in highly fast paced environments? I am used to the first alternative. Is it an advantage to switch?
Note: I originally posted this in Code Review and I was told that this topic belonged here. Kindly let me know if not.
So I have done a little bit with C++, but I would say the problem falls under the problem of namespaces in all languages. The real problem is that if you have multiple namespaces with the same function, it makes it much more difficult to read whats going on and can cause undesired results.
For example if you have two functions with the same name in two name spaces, how will the code know which one to use? Also another problem comes when you have multiple namespaces added and call a function. How does someone reading the code know which namespace the code is coming from? Having the namespace in front of the function helps make your code more readable.