Most viewed questions in C++

0 votes
1 answer

Examples of good gotos in C or C++

Here's one method I've heard of folks use. But I've never seen it in the wild.  And that only pertains to C since RAII allows you to accomplish this more idiomatically in C++. void foo() { if (!doA()) ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
426 views
0 votes
0 answers

What is the C++ function to raise a number to a power?

What's the best way to raise a n ...READ MORE

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
426 views
0 votes
0 answers

How come an array's address is equal to its value in C?

The pointer values and pointer addresses in ...READ MORE

Aug 8, 2022 in C++ by krishna
• 2,820 points
425 views
0 votes
0 answers

functions in c++ call by value and call by reference

The code below shows how to call a function in both methods.  Please explain the major differences or meanings of call by value and call by reference to me.  1.Make a value-based call.  2.Call based on a reference.  The call by value method is demonstrated in the following code. In a comment, I expressed my reservations. #include<iostream> int main(){ void change(int);//why function prototype is before ...READ MORE

Jun 6, 2022 in C++ by Nicholas
• 7,760 points
425 views
0 votes
1 answer

The new syntax "= default" in C++11

A defaulted default function Object() { [native code] } is defined as a user-defined default function Object() { [native code] } with an empty compound statement and no initialization list. I'll give you an example to demonstrate the difference: #include <iostream> using namespace std; class A { public: ...READ MORE

Jun 7, 2022 in C++ by Damon
• 4,960 points
425 views
0 votes
0 answers

C++ pointer to objects

Is it always necessary in C++ to  ...READ MORE

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
424 views
0 votes
1 answer

C++ "Object" class

No, there is no generic base class&nb ...READ MORE

Jun 2, 2022 in C++ by Damon
• 4,960 points
422 views
0 votes
0 answers

How to use the priority queue STL for objects?

class Person { public: int age; }; I'd want to put objects of the type Person in a priority queue. priority_queue< ...READ MORE

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
420 views
0 votes
1 answer

Are virtual functions the only way to achieve Runtime Polymorphism in C++?

fprintf is a polymorphism function in the C programming language. It can print to a file, stdout, a printer, a socket, or whatever else the system can represent as a stream if you supply it different handles. FILE* file = fopen("output.txt", "w"); ...READ MORE

Jun 21, 2022 in C++ by Damon
• 4,960 points
419 views
0 votes
0 answers

Sorting Characters Of A C++ String

Is there a built-in method for sorting characters in a string, or do I have to construct my own? for instance: string word = "dabc"; I would want to ...READ MORE

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
413 views
0 votes
1 answer

What is a lambda expression in C++11?

In C++11, what is a lambda expression? A: It's the object of an autogenerated class with overloading operator() const under the hood.  Closure is a type of object that is produced by the compiler.  This 'closure' idea is similar to C++11's bind notion.  Lambdas, on the other hand, usually produce better code.  Full inlining is also possible with calls through closures. Q: When do you think I'd utilise one? A: Define "simple and tiny logic" and request that the compiler generate the code from the preceding question.  You tell the compiler the expressions you wish to be inside the operator ().  The compiler will produce everything else for you. Q: What kind of problem do they tackle that couldn't be solved before they were introduced? A: It's some form of syntactic sugar, like using operators instead of functions for custom add, subtract, and other operations... However, wrapping 1-3 lines of genuine logic to some classes, and so on, saves additional lines of needless code!  Some engineers believe that if the number of lines is reduced, there is a lower likelihood of mistakes (which I agree with). Example of usage auto x = [=](int arg1){printf("%i", ...READ MORE

Jun 15, 2022 in C++ by Damon
• 4,960 points
409 views
0 votes
1 answer

What is a smart pointer and when should I use one?

A smart pointer is similar to a ...READ MORE

Jun 2, 2022 in C++ by Damon
• 4,960 points
403 views
0 votes
0 answers

Is the std::set iteration order always ascending according to the C++ specification?

According to what I've read, std::set in ...READ MORE

Jun 29, 2022 in C++ by Nicholas
• 7,760 points
402 views
0 votes
1 answer

Use of "this" keyword in C++ [duplicate]

Yes, it is optional and generally omitted.  However, it may be essential for accessing variables after they have been overridden in the scope: Person::Person() { int age; ...READ MORE

Jun 20, 2022 in C++ by Damon
• 4,960 points
402 views
0 votes
0 answers

What is meant with "const" at end of function declaration?

I received a book in which it is written: class Foo { public: int ...READ MORE

Jul 28, 2022 in C++ by Nicholas
• 7,760 points
400 views
0 votes
0 answers

What is the STL?

I'm not a C++ coder, thus I ...READ MORE

Jun 29, 2022 in C++ by Damon
• 4,960 points
400 views
0 votes
0 answers

memory allocation for objects

In C++, a variable like int x ...READ MORE

Aug 16, 2022 in C++ by Nicholas
• 7,760 points
398 views
0 votes
0 answers

In C, are arrays pointers or used as pointers?

First, look at this code When this is ...READ MORE

Aug 11, 2022 in C++ by krishna
• 2,820 points
392 views
0 votes
0 answers

Simple C++ swap function

Why is it that if I have a method like this to swap two numbers, it doesn't work[swap], (I know I can accomplish this by defining pointers in the prototype and then passing the addresses of the relevant variables in main()), yet it works for arrays without having to supply pointers and addresses? It does not work. void num_exchange(int m, int n); int main(){ int num1 ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
391 views
0 votes
0 answers

How to access static members of a class?

I'm learning C++ and Qt, but even the simplest code that I copy and paste from a book produces problems. On Ubuntu 10.04, I'm using g++4.4.2 with the QtCreator IDE.  Is there a distinction between the syntax of the g++ compiler and those of other compilers?  When I try to access static members, for example, something always goes wrong. #include <iostream> using namespace std; class A { ...READ MORE

Jul 7, 2022 in C++ by Nicholas
• 7,760 points
390 views
0 votes
0 answers

What are the differences between a pointer variable and a reference variable?

What distinguishes a reference variable from a ...READ MORE

Jul 11, 2022 in C++ by Nicholas
• 7,760 points
385 views
0 votes
0 answers

What are Containers/Adapters? C++

What exactly are containers/adapters? I am familiar ...READ MORE

Aug 8, 2022 in C++ by Nicholas
• 7,760 points
382 views
0 votes
1 answer

Use of multisets in C++ [duplicate]

Because a multi-set does not need the storage of single-element objects.  You're considering storing anything in a multi-set, such as a string.  But it is not its intended use.  You may use whatever struct you want and compare it to a single element in the struct. As an example: struct PhoneBookEntry { std::string name; ...READ MORE

Jun 27, 2022 in C++ by Damon
• 4,960 points
381 views
0 votes
0 answers

Why aren't cmath's pow and sqrt templates?

I'm curious as to why std::sqrt and ...READ MORE

Jul 13, 2022 in C++ by Nicholas
• 7,760 points
377 views
0 votes
0 answers

memory allocation for objects

When we instantiate a variable in C++, ...READ MORE

Aug 11, 2022 in C++ by Nicholas
• 7,760 points
376 views
0 votes
0 answers

How to concatenate two strings in C++?

I have a private class variable char ...READ MORE

Jul 25, 2022 in C++ by Nicholas
• 7,760 points
376 views
0 votes
0 answers

Difference between set.upper_bound() and upper_bound(set.begin(), set.end()) stl

I discovered that set.upper bound() was quicker ...READ MORE

Jun 29, 2022 in C++ by Damon
• 4,960 points
371 views
0 votes
0 answers

How to code a modulo (%) operator in C/C++/Obj-C that handles negative numbers

As a mathematician, one of my pet peeves of C-derived languages is : (-1) % 8 // comes out as ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
369 views
0 votes
0 answers

take string input using char* in C and C++

I'm having trouble accepting a string (actually a character array) as input.  Consider the following declaration: char* s; I have to input a string ...READ MORE

Jul 1, 2022 in C++ by Nicholas
• 7,760 points
367 views
0 votes
0 answers

How to generate a random number in C++?

I'm attempting to develop a dice game, and I need random numbers to represent the sides of the die.  I can make a number between 1 and 6).  Using #include <cstdlib> #include <ctime> #include <iostream> using namespace ...READ MORE

Aug 11, 2022 in C++ by Nicholas
• 7,760 points
364 views
0 votes
0 answers

What type of members can I add in a c++ abstract class

Hello, let's say I have an abstract class with a few pure abstract functions and a few classes that derive from it, and all of the data from these classes eventually becomes similar, I was wondering if it would be wise, or even possible, to declare a vector under protected in the abstract class to collect the data so something like that. class A { protected: vector <string> ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
364 views
0 votes
0 answers

not able to run graphics in DEV C++

Unable to run the graphics from following ...READ MORE

Jul 15, 2022 in C++ by Nicholas
• 7,760 points
357 views
0 votes
0 answers

Constructor Overloading in C++

My C++ overloading does not work as I expect it to: #include "Node.h" #include <iostream> Node::Node() { cout ...READ MORE

Jun 2, 2022 in C++ by Nicholas
• 7,760 points
357 views
0 votes
1 answer

Constructor Overloading in C++

In your function Object() { [native code] }, Node(Game(),v); does not work as expected.  It simply creates a temporary without actually using it, and has no effect.  When control passes over the ;, it immediately destroys the temporary. Initializing the members in each function Object() { [native code] } is the correct way to go.  You could put their shared code in a private init() member function and call it from each function Object() { [native code] }, as shown below: class Foo { public: ...READ MORE

Jun 21, 2022 in C++ by Damon
• 4,960 points
354 views
0 votes
0 answers

How to implement 2D vector array?

I'm using the vector class in the ...READ MORE

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
353 views
0 votes
0 answers

What is a stream in C++?

I've been hearing a lot about streams, ...READ MORE

Aug 11, 2022 in C++ by Nicholas
• 7,760 points
352 views
0 votes
0 answers

How to create a dynamic array of integers

How can I use the new keyword ...READ MORE

Jul 15, 2022 in C++ by Nicholas
• 7,760 points
351 views
0 votes
0 answers

What does '&' do in a C++ declaration?

As a C person, I'm attempting to comprehend some C++ code.  The declaration of my function is as follows: int foo(const string &myname) { cout ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
348 views
0 votes
1 answer

What is the name of the "<<" and ">>" operators? [duplicate]

According to cplusplus.com's documentation: This operator (<<) applied to ...READ MORE

Jun 20, 2022 in C++ by Damon
• 4,960 points
346 views
0 votes
0 answers

Why do we use volatile keyword?

I've never used it, but I'm curious ...READ MORE

Jul 25, 2022 in C++ by Nicholas
• 7,760 points
343 views
+1 vote
0 answers

Using push_back vs at in a vector in C++

I'm not sure how to use vectors in C++.  It has to do with the vector's push back technique.  I used push back to insert entries into the vector in the first programme.  I used at() to insert entries into the vector in the second application. #include <iostream> #include <vector> #include <string> using namespace std; int main ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
342 views
0 votes
0 answers

Why was the ampersand chosen as the symbol for references in C++?

Does anyone know why the ampersand was ...READ MORE

Aug 16, 2022 in C++ by Nicholas
• 7,760 points
341 views
0 votes
0 answers

What does the C++ standard state the size of int, long type to be?

I'm seeking for specific information on the sizes of basic C++ types.  I understand that it is determined by the architecture (16 bits, 32 bits, or 64 bits) and the compiler. But are there any C++ standards? On a 32-bit architecture, I'm using Visual Studio 2008.  This is what I get: char : 1 byte short : 2 ...READ MORE

Jul 5, 2022 in C++ by Nicholas
• 7,760 points
339 views
0 votes
0 answers

Can you use "cin" with string?

To enter a string, I was instructed to use gets(str) rather than cin.  In the programme below, though, I can use cin perfectly well.  Could you please tell me if you can use cin?  Please accept my apologies for my poor English.  The application allows you to enter up to five names and then print them on the screen. The code is as follows: #include <iostream> #include <string.h> using namespace std; int main() { ...READ MORE

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
338 views
0 votes
0 answers

what is containership or nesting in C++?

According to what I believed, a class ...READ MORE

Jul 11, 2022 in C++ by Nicholas
• 7,760 points
337 views
0 votes
0 answers

Why use #define instead of a variable

What is the purpose of the #define ...READ MORE

Jul 1, 2022 in C++ by Nicholas
• 7,760 points
334 views
0 votes
0 answers

Differences between C++ string == and compare()?

I just read some recommendations on using std::string ...READ MORE

Jul 7, 2022 in C++ by Nicholas
• 7,760 points
333 views
0 votes
0 answers

C++ Singleton design pattern

I recently came upon a C++ realization/implementation of the Singleton design pattern.  It looked like this (I took it from a real-life example): // a lot of methods are omitted ...READ MORE

Jul 4, 2022 in C++ by Nicholas
• 7,760 points
333 views
0 votes
0 answers

What are the new features in C++17?

C++17 is currently feature complete, therefore major ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
330 views
0 votes
0 answers

What are access specifiers? Should I inherit with private, protected or public?

I'm not sure what access modifiers signify ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
329 views