Create global variables for limited hardware resources, but with private constructor
I'm developing a C++ library for an embedded device. I want to create classes to handle some hardware resources such as UART peripherals.
What I want to do is to offer some global variables, for example UART1 and UART2. But I also want to keep the constructor private, because it makes no sense that the user can create a new instance for an UART that doesn't exist, or even worse, duplicate one of the existing one and trying to access it from 2 different objects.
The problem is that I can't access to the private constructor when defining them outside the class, like this.
// uart.h
class Uart {
private:
Uart(int uart_num);
}
extern Uart uart1, uart2;
// uart.c
Uart uart1(1);
Uart uart2(2);
What would be a good way to achieve this? I've been thinking about an static method like Uart &get_instance(int uart_num)
. This is not a bad option, but I like more the global variables for this, I see them cleaner and more elegant. If this is the best solution, should I return a pointer or a reference to the uart object?
I've also thought about static members of the class, but this makes too long strings to access the variables. My library is inside its own namespace so the user would have to access it writing mylibnamespace::Uart::uart1
, instead of mylibnamespace::uart1
. This is neither a bad solution, but I still prefer the global variables one.
c++ global-variables
|
show 2 more comments
I'm developing a C++ library for an embedded device. I want to create classes to handle some hardware resources such as UART peripherals.
What I want to do is to offer some global variables, for example UART1 and UART2. But I also want to keep the constructor private, because it makes no sense that the user can create a new instance for an UART that doesn't exist, or even worse, duplicate one of the existing one and trying to access it from 2 different objects.
The problem is that I can't access to the private constructor when defining them outside the class, like this.
// uart.h
class Uart {
private:
Uart(int uart_num);
}
extern Uart uart1, uart2;
// uart.c
Uart uart1(1);
Uart uart2(2);
What would be a good way to achieve this? I've been thinking about an static method like Uart &get_instance(int uart_num)
. This is not a bad option, but I like more the global variables for this, I see them cleaner and more elegant. If this is the best solution, should I return a pointer or a reference to the uart object?
I've also thought about static members of the class, but this makes too long strings to access the variables. My library is inside its own namespace so the user would have to access it writing mylibnamespace::Uart::uart1
, instead of mylibnamespace::uart1
. This is neither a bad solution, but I still prefer the global variables one.
c++ global-variables
2
You're looking for a factory, essentially what you described in yourstatic
method part.
– Tas
Nov 12 '18 at 11:11
1
You can declare a friend function - which can then access your private constructor - and have that function as your factory (if that meets your needs.)
– Rags
Nov 12 '18 at 11:12
You can always have static functions, and then you add a wrapper in the parent scope, to avoid friend.
– Matthieu Brucher
Nov 12 '18 at 11:23
Depending on the information you want to share, you could use "get-apis" instead of public variables.
– Jose
Nov 12 '18 at 11:28
@Rags that doesn't work because when defining the variable default constructor is called, and it's private. It should work with pointers and allocating in the heap, I supose.
– naggety
Nov 12 '18 at 11:57
|
show 2 more comments
I'm developing a C++ library for an embedded device. I want to create classes to handle some hardware resources such as UART peripherals.
What I want to do is to offer some global variables, for example UART1 and UART2. But I also want to keep the constructor private, because it makes no sense that the user can create a new instance for an UART that doesn't exist, or even worse, duplicate one of the existing one and trying to access it from 2 different objects.
The problem is that I can't access to the private constructor when defining them outside the class, like this.
// uart.h
class Uart {
private:
Uart(int uart_num);
}
extern Uart uart1, uart2;
// uart.c
Uart uart1(1);
Uart uart2(2);
What would be a good way to achieve this? I've been thinking about an static method like Uart &get_instance(int uart_num)
. This is not a bad option, but I like more the global variables for this, I see them cleaner and more elegant. If this is the best solution, should I return a pointer or a reference to the uart object?
I've also thought about static members of the class, but this makes too long strings to access the variables. My library is inside its own namespace so the user would have to access it writing mylibnamespace::Uart::uart1
, instead of mylibnamespace::uart1
. This is neither a bad solution, but I still prefer the global variables one.
c++ global-variables
I'm developing a C++ library for an embedded device. I want to create classes to handle some hardware resources such as UART peripherals.
What I want to do is to offer some global variables, for example UART1 and UART2. But I also want to keep the constructor private, because it makes no sense that the user can create a new instance for an UART that doesn't exist, or even worse, duplicate one of the existing one and trying to access it from 2 different objects.
The problem is that I can't access to the private constructor when defining them outside the class, like this.
// uart.h
class Uart {
private:
Uart(int uart_num);
}
extern Uart uart1, uart2;
// uart.c
Uart uart1(1);
Uart uart2(2);
What would be a good way to achieve this? I've been thinking about an static method like Uart &get_instance(int uart_num)
. This is not a bad option, but I like more the global variables for this, I see them cleaner and more elegant. If this is the best solution, should I return a pointer or a reference to the uart object?
I've also thought about static members of the class, but this makes too long strings to access the variables. My library is inside its own namespace so the user would have to access it writing mylibnamespace::Uart::uart1
, instead of mylibnamespace::uart1
. This is neither a bad solution, but I still prefer the global variables one.
c++ global-variables
c++ global-variables
asked Nov 12 '18 at 11:05
naggetynaggety
13617
13617
2
You're looking for a factory, essentially what you described in yourstatic
method part.
– Tas
Nov 12 '18 at 11:11
1
You can declare a friend function - which can then access your private constructor - and have that function as your factory (if that meets your needs.)
– Rags
Nov 12 '18 at 11:12
You can always have static functions, and then you add a wrapper in the parent scope, to avoid friend.
– Matthieu Brucher
Nov 12 '18 at 11:23
Depending on the information you want to share, you could use "get-apis" instead of public variables.
– Jose
Nov 12 '18 at 11:28
@Rags that doesn't work because when defining the variable default constructor is called, and it's private. It should work with pointers and allocating in the heap, I supose.
– naggety
Nov 12 '18 at 11:57
|
show 2 more comments
2
You're looking for a factory, essentially what you described in yourstatic
method part.
– Tas
Nov 12 '18 at 11:11
1
You can declare a friend function - which can then access your private constructor - and have that function as your factory (if that meets your needs.)
– Rags
Nov 12 '18 at 11:12
You can always have static functions, and then you add a wrapper in the parent scope, to avoid friend.
– Matthieu Brucher
Nov 12 '18 at 11:23
Depending on the information you want to share, you could use "get-apis" instead of public variables.
– Jose
Nov 12 '18 at 11:28
@Rags that doesn't work because when defining the variable default constructor is called, and it's private. It should work with pointers and allocating in the heap, I supose.
– naggety
Nov 12 '18 at 11:57
2
2
You're looking for a factory, essentially what you described in your
static
method part.– Tas
Nov 12 '18 at 11:11
You're looking for a factory, essentially what you described in your
static
method part.– Tas
Nov 12 '18 at 11:11
1
1
You can declare a friend function - which can then access your private constructor - and have that function as your factory (if that meets your needs.)
– Rags
Nov 12 '18 at 11:12
You can declare a friend function - which can then access your private constructor - and have that function as your factory (if that meets your needs.)
– Rags
Nov 12 '18 at 11:12
You can always have static functions, and then you add a wrapper in the parent scope, to avoid friend.
– Matthieu Brucher
Nov 12 '18 at 11:23
You can always have static functions, and then you add a wrapper in the parent scope, to avoid friend.
– Matthieu Brucher
Nov 12 '18 at 11:23
Depending on the information you want to share, you could use "get-apis" instead of public variables.
– Jose
Nov 12 '18 at 11:28
Depending on the information you want to share, you could use "get-apis" instead of public variables.
– Jose
Nov 12 '18 at 11:28
@Rags that doesn't work because when defining the variable default constructor is called, and it's private. It should work with pointers and allocating in the heap, I supose.
– naggety
Nov 12 '18 at 11:57
@Rags that doesn't work because when defining the variable default constructor is called, and it's private. It should work with pointers and allocating in the heap, I supose.
– naggety
Nov 12 '18 at 11:57
|
show 2 more comments
2 Answers
2
active
oldest
votes
I don't see why it is better for you to use global variables, I would use something more dynamic with pointeres or the factory solution suggested above. But, anyways, The only way I see to provide global variables with no constructor (or private) is creating from an inherit class using default copy constructor. Since this is not a pointer or a reference you should be very carefull, this UartPriv::UartPriv(int) constructor is just to fill Uart member variables, not additional methods or variables should be declared here.
// uart.h
class Uart{
};
Uart uart1, uart2;
// uart.c
class UartPriv : public Uart{
public:
UartPriv( int uart )
{
//initialize Uart members here
}
};
Uart Uart1 = UartPriv( 1 );
Uart Uart2 = UartPriv( 2 );
This is a good idea, but unfortunately it doesn't work because I have to delete the move and copy constructors to avoid other instances to be created anywhere, so it doesn't work.
– naggety
Nov 16 '18 at 9:11
add a comment |
Let's create a full answer. I will avoid using friend declaration here.
class Uart {
public:
static Uart& get(int uart_num);
Uart(const Uart&) = delete;
Uart(Uart&&) = delete;
private:
Uart(int uart_num);
}
// This is the function you can use from the outside
Uart& get_Uart(int uart_num);
Uart Uart::get(int uart_num)
{
Uart uart0(0);
switch(uart_num):
{
case 0:
return uart0;
};
throw std::out_of_range("no such UART");
}
Uart& get_Uart(uart_num)
{
return Uart::get(uart_num);
}
Uart& foo = get_Uart(foo_num);
This doesn't meet the requirements because this way new instances can be constructed calling Uart::construct from anywhere. The same would happen using friend declaration because the friend function must be global.
– naggety
Nov 16 '18 at 9:04
Thanks for the effort of offering a solution, but it doesn't answer the question. What I need is a public API which doesn't offer the chance of creating new Uart instances, but just use the public methods of some already created Uart instances.
– naggety
Nov 16 '18 at 10:50
OK, let's try something else then.
– Matthieu Brucher
Nov 16 '18 at 10:55
The good aspect of the static variables inside the getter is that you don't have any initialization race. You can enforce the order in which they are initialized.
– Matthieu Brucher
Nov 16 '18 at 10:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53260836%2fcreate-global-variables-for-limited-hardware-resources-but-with-private-constru%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I don't see why it is better for you to use global variables, I would use something more dynamic with pointeres or the factory solution suggested above. But, anyways, The only way I see to provide global variables with no constructor (or private) is creating from an inherit class using default copy constructor. Since this is not a pointer or a reference you should be very carefull, this UartPriv::UartPriv(int) constructor is just to fill Uart member variables, not additional methods or variables should be declared here.
// uart.h
class Uart{
};
Uart uart1, uart2;
// uart.c
class UartPriv : public Uart{
public:
UartPriv( int uart )
{
//initialize Uart members here
}
};
Uart Uart1 = UartPriv( 1 );
Uart Uart2 = UartPriv( 2 );
This is a good idea, but unfortunately it doesn't work because I have to delete the move and copy constructors to avoid other instances to be created anywhere, so it doesn't work.
– naggety
Nov 16 '18 at 9:11
add a comment |
I don't see why it is better for you to use global variables, I would use something more dynamic with pointeres or the factory solution suggested above. But, anyways, The only way I see to provide global variables with no constructor (or private) is creating from an inherit class using default copy constructor. Since this is not a pointer or a reference you should be very carefull, this UartPriv::UartPriv(int) constructor is just to fill Uart member variables, not additional methods or variables should be declared here.
// uart.h
class Uart{
};
Uart uart1, uart2;
// uart.c
class UartPriv : public Uart{
public:
UartPriv( int uart )
{
//initialize Uart members here
}
};
Uart Uart1 = UartPriv( 1 );
Uart Uart2 = UartPriv( 2 );
This is a good idea, but unfortunately it doesn't work because I have to delete the move and copy constructors to avoid other instances to be created anywhere, so it doesn't work.
– naggety
Nov 16 '18 at 9:11
add a comment |
I don't see why it is better for you to use global variables, I would use something more dynamic with pointeres or the factory solution suggested above. But, anyways, The only way I see to provide global variables with no constructor (or private) is creating from an inherit class using default copy constructor. Since this is not a pointer or a reference you should be very carefull, this UartPriv::UartPriv(int) constructor is just to fill Uart member variables, not additional methods or variables should be declared here.
// uart.h
class Uart{
};
Uart uart1, uart2;
// uart.c
class UartPriv : public Uart{
public:
UartPriv( int uart )
{
//initialize Uart members here
}
};
Uart Uart1 = UartPriv( 1 );
Uart Uart2 = UartPriv( 2 );
I don't see why it is better for you to use global variables, I would use something more dynamic with pointeres or the factory solution suggested above. But, anyways, The only way I see to provide global variables with no constructor (or private) is creating from an inherit class using default copy constructor. Since this is not a pointer or a reference you should be very carefull, this UartPriv::UartPriv(int) constructor is just to fill Uart member variables, not additional methods or variables should be declared here.
// uart.h
class Uart{
};
Uart uart1, uart2;
// uart.c
class UartPriv : public Uart{
public:
UartPriv( int uart )
{
//initialize Uart members here
}
};
Uart Uart1 = UartPriv( 1 );
Uart Uart2 = UartPriv( 2 );
answered Nov 12 '18 at 12:40
Pablo AlegrePablo Alegre
393
393
This is a good idea, but unfortunately it doesn't work because I have to delete the move and copy constructors to avoid other instances to be created anywhere, so it doesn't work.
– naggety
Nov 16 '18 at 9:11
add a comment |
This is a good idea, but unfortunately it doesn't work because I have to delete the move and copy constructors to avoid other instances to be created anywhere, so it doesn't work.
– naggety
Nov 16 '18 at 9:11
This is a good idea, but unfortunately it doesn't work because I have to delete the move and copy constructors to avoid other instances to be created anywhere, so it doesn't work.
– naggety
Nov 16 '18 at 9:11
This is a good idea, but unfortunately it doesn't work because I have to delete the move and copy constructors to avoid other instances to be created anywhere, so it doesn't work.
– naggety
Nov 16 '18 at 9:11
add a comment |
Let's create a full answer. I will avoid using friend declaration here.
class Uart {
public:
static Uart& get(int uart_num);
Uart(const Uart&) = delete;
Uart(Uart&&) = delete;
private:
Uart(int uart_num);
}
// This is the function you can use from the outside
Uart& get_Uart(int uart_num);
Uart Uart::get(int uart_num)
{
Uart uart0(0);
switch(uart_num):
{
case 0:
return uart0;
};
throw std::out_of_range("no such UART");
}
Uart& get_Uart(uart_num)
{
return Uart::get(uart_num);
}
Uart& foo = get_Uart(foo_num);
This doesn't meet the requirements because this way new instances can be constructed calling Uart::construct from anywhere. The same would happen using friend declaration because the friend function must be global.
– naggety
Nov 16 '18 at 9:04
Thanks for the effort of offering a solution, but it doesn't answer the question. What I need is a public API which doesn't offer the chance of creating new Uart instances, but just use the public methods of some already created Uart instances.
– naggety
Nov 16 '18 at 10:50
OK, let's try something else then.
– Matthieu Brucher
Nov 16 '18 at 10:55
The good aspect of the static variables inside the getter is that you don't have any initialization race. You can enforce the order in which they are initialized.
– Matthieu Brucher
Nov 16 '18 at 10:59
add a comment |
Let's create a full answer. I will avoid using friend declaration here.
class Uart {
public:
static Uart& get(int uart_num);
Uart(const Uart&) = delete;
Uart(Uart&&) = delete;
private:
Uart(int uart_num);
}
// This is the function you can use from the outside
Uart& get_Uart(int uart_num);
Uart Uart::get(int uart_num)
{
Uart uart0(0);
switch(uart_num):
{
case 0:
return uart0;
};
throw std::out_of_range("no such UART");
}
Uart& get_Uart(uart_num)
{
return Uart::get(uart_num);
}
Uart& foo = get_Uart(foo_num);
This doesn't meet the requirements because this way new instances can be constructed calling Uart::construct from anywhere. The same would happen using friend declaration because the friend function must be global.
– naggety
Nov 16 '18 at 9:04
Thanks for the effort of offering a solution, but it doesn't answer the question. What I need is a public API which doesn't offer the chance of creating new Uart instances, but just use the public methods of some already created Uart instances.
– naggety
Nov 16 '18 at 10:50
OK, let's try something else then.
– Matthieu Brucher
Nov 16 '18 at 10:55
The good aspect of the static variables inside the getter is that you don't have any initialization race. You can enforce the order in which they are initialized.
– Matthieu Brucher
Nov 16 '18 at 10:59
add a comment |
Let's create a full answer. I will avoid using friend declaration here.
class Uart {
public:
static Uart& get(int uart_num);
Uart(const Uart&) = delete;
Uart(Uart&&) = delete;
private:
Uart(int uart_num);
}
// This is the function you can use from the outside
Uart& get_Uart(int uart_num);
Uart Uart::get(int uart_num)
{
Uart uart0(0);
switch(uart_num):
{
case 0:
return uart0;
};
throw std::out_of_range("no such UART");
}
Uart& get_Uart(uart_num)
{
return Uart::get(uart_num);
}
Uart& foo = get_Uart(foo_num);
Let's create a full answer. I will avoid using friend declaration here.
class Uart {
public:
static Uart& get(int uart_num);
Uart(const Uart&) = delete;
Uart(Uart&&) = delete;
private:
Uart(int uart_num);
}
// This is the function you can use from the outside
Uart& get_Uart(int uart_num);
Uart Uart::get(int uart_num)
{
Uart uart0(0);
switch(uart_num):
{
case 0:
return uart0;
};
throw std::out_of_range("no such UART");
}
Uart& get_Uart(uart_num)
{
return Uart::get(uart_num);
}
Uart& foo = get_Uart(foo_num);
edited Nov 16 '18 at 10:58
answered Nov 12 '18 at 12:02
Matthieu BrucherMatthieu Brucher
12.9k22140
12.9k22140
This doesn't meet the requirements because this way new instances can be constructed calling Uart::construct from anywhere. The same would happen using friend declaration because the friend function must be global.
– naggety
Nov 16 '18 at 9:04
Thanks for the effort of offering a solution, but it doesn't answer the question. What I need is a public API which doesn't offer the chance of creating new Uart instances, but just use the public methods of some already created Uart instances.
– naggety
Nov 16 '18 at 10:50
OK, let's try something else then.
– Matthieu Brucher
Nov 16 '18 at 10:55
The good aspect of the static variables inside the getter is that you don't have any initialization race. You can enforce the order in which they are initialized.
– Matthieu Brucher
Nov 16 '18 at 10:59
add a comment |
This doesn't meet the requirements because this way new instances can be constructed calling Uart::construct from anywhere. The same would happen using friend declaration because the friend function must be global.
– naggety
Nov 16 '18 at 9:04
Thanks for the effort of offering a solution, but it doesn't answer the question. What I need is a public API which doesn't offer the chance of creating new Uart instances, but just use the public methods of some already created Uart instances.
– naggety
Nov 16 '18 at 10:50
OK, let's try something else then.
– Matthieu Brucher
Nov 16 '18 at 10:55
The good aspect of the static variables inside the getter is that you don't have any initialization race. You can enforce the order in which they are initialized.
– Matthieu Brucher
Nov 16 '18 at 10:59
This doesn't meet the requirements because this way new instances can be constructed calling Uart::construct from anywhere. The same would happen using friend declaration because the friend function must be global.
– naggety
Nov 16 '18 at 9:04
This doesn't meet the requirements because this way new instances can be constructed calling Uart::construct from anywhere. The same would happen using friend declaration because the friend function must be global.
– naggety
Nov 16 '18 at 9:04
Thanks for the effort of offering a solution, but it doesn't answer the question. What I need is a public API which doesn't offer the chance of creating new Uart instances, but just use the public methods of some already created Uart instances.
– naggety
Nov 16 '18 at 10:50
Thanks for the effort of offering a solution, but it doesn't answer the question. What I need is a public API which doesn't offer the chance of creating new Uart instances, but just use the public methods of some already created Uart instances.
– naggety
Nov 16 '18 at 10:50
OK, let's try something else then.
– Matthieu Brucher
Nov 16 '18 at 10:55
OK, let's try something else then.
– Matthieu Brucher
Nov 16 '18 at 10:55
The good aspect of the static variables inside the getter is that you don't have any initialization race. You can enforce the order in which they are initialized.
– Matthieu Brucher
Nov 16 '18 at 10:59
The good aspect of the static variables inside the getter is that you don't have any initialization race. You can enforce the order in which they are initialized.
– Matthieu Brucher
Nov 16 '18 at 10:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53260836%2fcreate-global-variables-for-limited-hardware-resources-but-with-private-constru%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You're looking for a factory, essentially what you described in your
static
method part.– Tas
Nov 12 '18 at 11:11
1
You can declare a friend function - which can then access your private constructor - and have that function as your factory (if that meets your needs.)
– Rags
Nov 12 '18 at 11:12
You can always have static functions, and then you add a wrapper in the parent scope, to avoid friend.
– Matthieu Brucher
Nov 12 '18 at 11:23
Depending on the information you want to share, you could use "get-apis" instead of public variables.
– Jose
Nov 12 '18 at 11:28
@Rags that doesn't work because when defining the variable default constructor is called, and it's private. It should work with pointers and allocating in the heap, I supose.
– naggety
Nov 12 '18 at 11:57