C++ Nested class (Make friend outer member function)?
My code:
class Controller {
private:
class ControllerMetals {
private:
int m_size;
Metals * m_metals;
public:
ControllerMetals();
Metals & getMetals() const;
int getSize() const;
void setSize(int size) { m_size = size; }
void init();
void show();
void erase();
friend void Controller::start(ControllerMetals & c); // why don't work ?
};
private:
ControllerMetals * controlMetals;
public:
void start(ControllerMetals & c);
ControllerMetals * getControlMetals() const;
Controller();
};
I want to make a void start to have access private member in ControllerMetals class. Why friend statements don't work ?
c++ class nested friend
add a comment |
My code:
class Controller {
private:
class ControllerMetals {
private:
int m_size;
Metals * m_metals;
public:
ControllerMetals();
Metals & getMetals() const;
int getSize() const;
void setSize(int size) { m_size = size; }
void init();
void show();
void erase();
friend void Controller::start(ControllerMetals & c); // why don't work ?
};
private:
ControllerMetals * controlMetals;
public:
void start(ControllerMetals & c);
ControllerMetals * getControlMetals() const;
Controller();
};
I want to make a void start to have access private member in ControllerMetals class. Why friend statements don't work ?
c++ class nested friend
1
Since ControllerMetals is private inside of Controller, why not make everything public in ControllerMetals? Obviously the nested private class is tightly coupled with the outer class.
– Eljay
Nov 22 '18 at 22:25
I have a task and I must declare my inner class as private member of outer class. And all member must be private.
– loczek
Nov 22 '18 at 22:56
Controller::start
doesn't exist when you friend it. You can declare a datatype when you friend it but I don't think you can with a function. Forward declareControllerMetals
so you can refer to it and then formally define it after the functions. Why the hell didn't I just make an answer?
– user4581301
Nov 23 '18 at 1:48
add a comment |
My code:
class Controller {
private:
class ControllerMetals {
private:
int m_size;
Metals * m_metals;
public:
ControllerMetals();
Metals & getMetals() const;
int getSize() const;
void setSize(int size) { m_size = size; }
void init();
void show();
void erase();
friend void Controller::start(ControllerMetals & c); // why don't work ?
};
private:
ControllerMetals * controlMetals;
public:
void start(ControllerMetals & c);
ControllerMetals * getControlMetals() const;
Controller();
};
I want to make a void start to have access private member in ControllerMetals class. Why friend statements don't work ?
c++ class nested friend
My code:
class Controller {
private:
class ControllerMetals {
private:
int m_size;
Metals * m_metals;
public:
ControllerMetals();
Metals & getMetals() const;
int getSize() const;
void setSize(int size) { m_size = size; }
void init();
void show();
void erase();
friend void Controller::start(ControllerMetals & c); // why don't work ?
};
private:
ControllerMetals * controlMetals;
public:
void start(ControllerMetals & c);
ControllerMetals * getControlMetals() const;
Controller();
};
I want to make a void start to have access private member in ControllerMetals class. Why friend statements don't work ?
c++ class nested friend
c++ class nested friend
asked Nov 22 '18 at 22:11
loczek
83
83
1
Since ControllerMetals is private inside of Controller, why not make everything public in ControllerMetals? Obviously the nested private class is tightly coupled with the outer class.
– Eljay
Nov 22 '18 at 22:25
I have a task and I must declare my inner class as private member of outer class. And all member must be private.
– loczek
Nov 22 '18 at 22:56
Controller::start
doesn't exist when you friend it. You can declare a datatype when you friend it but I don't think you can with a function. Forward declareControllerMetals
so you can refer to it and then formally define it after the functions. Why the hell didn't I just make an answer?
– user4581301
Nov 23 '18 at 1:48
add a comment |
1
Since ControllerMetals is private inside of Controller, why not make everything public in ControllerMetals? Obviously the nested private class is tightly coupled with the outer class.
– Eljay
Nov 22 '18 at 22:25
I have a task and I must declare my inner class as private member of outer class. And all member must be private.
– loczek
Nov 22 '18 at 22:56
Controller::start
doesn't exist when you friend it. You can declare a datatype when you friend it but I don't think you can with a function. Forward declareControllerMetals
so you can refer to it and then formally define it after the functions. Why the hell didn't I just make an answer?
– user4581301
Nov 23 '18 at 1:48
1
1
Since ControllerMetals is private inside of Controller, why not make everything public in ControllerMetals? Obviously the nested private class is tightly coupled with the outer class.
– Eljay
Nov 22 '18 at 22:25
Since ControllerMetals is private inside of Controller, why not make everything public in ControllerMetals? Obviously the nested private class is tightly coupled with the outer class.
– Eljay
Nov 22 '18 at 22:25
I have a task and I must declare my inner class as private member of outer class. And all member must be private.
– loczek
Nov 22 '18 at 22:56
I have a task and I must declare my inner class as private member of outer class. And all member must be private.
– loczek
Nov 22 '18 at 22:56
Controller::start
doesn't exist when you friend it. You can declare a datatype when you friend it but I don't think you can with a function. Forward declare ControllerMetals
so you can refer to it and then formally define it after the functions. Why the hell didn't I just make an answer?– user4581301
Nov 23 '18 at 1:48
Controller::start
doesn't exist when you friend it. You can declare a datatype when you friend it but I don't think you can with a function. Forward declare ControllerMetals
so you can refer to it and then formally define it after the functions. Why the hell didn't I just make an answer?– user4581301
Nov 23 '18 at 1:48
add a comment |
1 Answer
1
active
oldest
votes
Problem
Member functions have to be declared before you can friend
them. friend
has a built-in forward declaration for a data type, but not for members of that data type.
Solution
Personally I agree with Eljay's comment, make everything in ControllerMetals
public
because it is already hidden by Controller
, but if the assignment says no, do what you have to do to pass the course.
Simple solution:
You friend
the whole Controller
class to get the members, but this might be too broad.
More complicated, tighter-grained solution:
More stuff around so that the required member function is declared before ControllerMetals
. You can get away with this because the start
only needs a declaration of ControllerMetals
in order to take references to it.
class Controller {
class ControllerMetals; // forward declare to make available for referencing
public:
void start(ControllerMetals & c); // start now known. Can friend
ControllerMetals * getControlMetals() const;
Controller();
private:
// now we can fully define ControllerMetals
class ControllerMetals {
private:
int m_size;
Metals * m_metals;
public:
ControllerMetals();
Metals & getMetals() const;
int getSize() const;
void setSize(int size) { m_size = size; }
void init(); // why is this not done in the constructor?
void show();
void erase();
friend void Controller::start(ControllerMetals & c); // now works
};
ControllerMetals * controlMetals;
};
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%2f53438526%2fc-nested-class-make-friend-outer-member-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Problem
Member functions have to be declared before you can friend
them. friend
has a built-in forward declaration for a data type, but not for members of that data type.
Solution
Personally I agree with Eljay's comment, make everything in ControllerMetals
public
because it is already hidden by Controller
, but if the assignment says no, do what you have to do to pass the course.
Simple solution:
You friend
the whole Controller
class to get the members, but this might be too broad.
More complicated, tighter-grained solution:
More stuff around so that the required member function is declared before ControllerMetals
. You can get away with this because the start
only needs a declaration of ControllerMetals
in order to take references to it.
class Controller {
class ControllerMetals; // forward declare to make available for referencing
public:
void start(ControllerMetals & c); // start now known. Can friend
ControllerMetals * getControlMetals() const;
Controller();
private:
// now we can fully define ControllerMetals
class ControllerMetals {
private:
int m_size;
Metals * m_metals;
public:
ControllerMetals();
Metals & getMetals() const;
int getSize() const;
void setSize(int size) { m_size = size; }
void init(); // why is this not done in the constructor?
void show();
void erase();
friend void Controller::start(ControllerMetals & c); // now works
};
ControllerMetals * controlMetals;
};
add a comment |
Problem
Member functions have to be declared before you can friend
them. friend
has a built-in forward declaration for a data type, but not for members of that data type.
Solution
Personally I agree with Eljay's comment, make everything in ControllerMetals
public
because it is already hidden by Controller
, but if the assignment says no, do what you have to do to pass the course.
Simple solution:
You friend
the whole Controller
class to get the members, but this might be too broad.
More complicated, tighter-grained solution:
More stuff around so that the required member function is declared before ControllerMetals
. You can get away with this because the start
only needs a declaration of ControllerMetals
in order to take references to it.
class Controller {
class ControllerMetals; // forward declare to make available for referencing
public:
void start(ControllerMetals & c); // start now known. Can friend
ControllerMetals * getControlMetals() const;
Controller();
private:
// now we can fully define ControllerMetals
class ControllerMetals {
private:
int m_size;
Metals * m_metals;
public:
ControllerMetals();
Metals & getMetals() const;
int getSize() const;
void setSize(int size) { m_size = size; }
void init(); // why is this not done in the constructor?
void show();
void erase();
friend void Controller::start(ControllerMetals & c); // now works
};
ControllerMetals * controlMetals;
};
add a comment |
Problem
Member functions have to be declared before you can friend
them. friend
has a built-in forward declaration for a data type, but not for members of that data type.
Solution
Personally I agree with Eljay's comment, make everything in ControllerMetals
public
because it is already hidden by Controller
, but if the assignment says no, do what you have to do to pass the course.
Simple solution:
You friend
the whole Controller
class to get the members, but this might be too broad.
More complicated, tighter-grained solution:
More stuff around so that the required member function is declared before ControllerMetals
. You can get away with this because the start
only needs a declaration of ControllerMetals
in order to take references to it.
class Controller {
class ControllerMetals; // forward declare to make available for referencing
public:
void start(ControllerMetals & c); // start now known. Can friend
ControllerMetals * getControlMetals() const;
Controller();
private:
// now we can fully define ControllerMetals
class ControllerMetals {
private:
int m_size;
Metals * m_metals;
public:
ControllerMetals();
Metals & getMetals() const;
int getSize() const;
void setSize(int size) { m_size = size; }
void init(); // why is this not done in the constructor?
void show();
void erase();
friend void Controller::start(ControllerMetals & c); // now works
};
ControllerMetals * controlMetals;
};
Problem
Member functions have to be declared before you can friend
them. friend
has a built-in forward declaration for a data type, but not for members of that data type.
Solution
Personally I agree with Eljay's comment, make everything in ControllerMetals
public
because it is already hidden by Controller
, but if the assignment says no, do what you have to do to pass the course.
Simple solution:
You friend
the whole Controller
class to get the members, but this might be too broad.
More complicated, tighter-grained solution:
More stuff around so that the required member function is declared before ControllerMetals
. You can get away with this because the start
only needs a declaration of ControllerMetals
in order to take references to it.
class Controller {
class ControllerMetals; // forward declare to make available for referencing
public:
void start(ControllerMetals & c); // start now known. Can friend
ControllerMetals * getControlMetals() const;
Controller();
private:
// now we can fully define ControllerMetals
class ControllerMetals {
private:
int m_size;
Metals * m_metals;
public:
ControllerMetals();
Metals & getMetals() const;
int getSize() const;
void setSize(int size) { m_size = size; }
void init(); // why is this not done in the constructor?
void show();
void erase();
friend void Controller::start(ControllerMetals & c); // now works
};
ControllerMetals * controlMetals;
};
edited Nov 23 '18 at 15:30
answered Nov 23 '18 at 1:53
user4581301
19.5k51831
19.5k51831
add a comment |
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%2f53438526%2fc-nested-class-make-friend-outer-member-function%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
1
Since ControllerMetals is private inside of Controller, why not make everything public in ControllerMetals? Obviously the nested private class is tightly coupled with the outer class.
– Eljay
Nov 22 '18 at 22:25
I have a task and I must declare my inner class as private member of outer class. And all member must be private.
– loczek
Nov 22 '18 at 22:56
Controller::start
doesn't exist when you friend it. You can declare a datatype when you friend it but I don't think you can with a function. Forward declareControllerMetals
so you can refer to it and then formally define it after the functions. Why the hell didn't I just make an answer?– user4581301
Nov 23 '18 at 1:48