All Questions
577 questions
2
votes
1
answer
98
views
Can one pass by value an object with private destructor in a function?
If a class has inaccessible (private) destructor, can it be passed by value to a function as in the following example?
class A {
~A() {}
};
// ok in GCC and Clang, error in MSVC
void f(A) {}
GCC ...
0
votes
1
answer
226
views
how to return a std::expect<T, E> from a function where T's move/copy constructor are deleted and has a private constructor
Currently I have this code, which works fine:
#pragma once
#include <expected>
#include <wayland-server.h>
class Display
{
private:
struct wl_display *ptr;
public:
enum class ...
-1
votes
1
answer
69
views
Is there a way to add a friend class that is itself but with different template arguments? [duplicate]
I was writing a numpy-equivalent variable-dimension array in C++. I used a template class to store different kinds of datatypes in the array.
Eventually, I had to write a code for the datatype ...
-2
votes
2
answers
676
views
Assign a value to a variable in private section of class in C++
How can you assign a variable in the private section of a class in C++?
I tried to do it the following way:
private:
std::string foo;
foo = "randomValue";
But that results in an ...
3
votes
1
answer
111
views
Why is public method in private struct accessible
Why does this code compile and lib::Foo::SecretStruct::SecretFunc can be accessed despite SecretStruct being private?
#include <iostream>
#include <memory>
#include <type_traits>
...
-1
votes
1
answer
156
views
Hard private state in C++ [closed]
Assuming the platform doesn't expose another means of peeking at memory locations (I know C++ is usually compiled to actual machine code), is it possible, the way it is in e.g. JavaScript?
I know ...
0
votes
1
answer
1k
views
CMake Library Target, Public and Private Headers in Single Directory
Assume I have the following project structure (inherited code - cannot change structure):
MyProject
├── CMakeLists.txt
├── src
│ └── foo.cpp
│ └── bar.cpp
├── include
│ └── foo_public.h
│ └── ...
-3
votes
1
answer
941
views
Define a private variable in a header file or cpp file without using class?
Hi I have a question that, without using class, is there any other way to define a 'private' variable which can only be seen inside of a module, so that when we include different modules into a file, ...
5
votes
2
answers
100
views
Why is a private member visible when initializing a static variable?
This compiles:
class Node{
int m = 0;
static unsigned f(){
return 1;
}
public:
static unsigned a;
static unsigned b;
};
unsigned Node::a = sizeof(m); // <= !!!
...
-1
votes
2
answers
179
views
How to initialize private static constant array of the same class C++
I need to make class B to work with graph of related objects of class A. An instance of B contains A by value, and links to linked B objects(array indexes). Array of B must to be private as OOP ...
-2
votes
1
answer
232
views
alternative copy constructor in c++
Edit:
I am going to introduce some badly designed class, this was an old question and I have grown as a programmer since then...
Let's say I have a class with some state and with a method that only ...
0
votes
1
answer
73
views
strcpy access private object in class
I'm reading data from a file in JSON format, and I want to put the values in the objects password_c and apiKeyValue members:
class Configuration {
private:
char password_c[32];
char ...
0
votes
1
answer
75
views
Accessing private class within a code error
Hello I need help in accessing and displaying the results of the three classes derived from the base class. They are private and I'm having difficulty in doing so. I have searched and tried on my own ...
0
votes
1
answer
116
views
What is the use of a private init() member function?
EDIT for more details: this is a school assignment and the private variables/functions of the Time class were given to me already. I have to declare and define a member function which adds two Times ...
0
votes
0
answers
226
views
are friend function a good practice in this case>? [duplicate]
I have a few questions about good practices,
I have this piece of code in c++:
class Complex{
friend double Re(Complex &z);
friend double Im(Complex &z);
friend Complex ...