C++ Nested class (Make friend outer member function)?












-2














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 ?










share|improve this question


















  • 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 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
















-2














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 ?










share|improve this question


















  • 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 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














-2












-2








-2







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 ?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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














  • 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 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








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












1 Answer
1






active

oldest

votes


















1














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;


};





share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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;


    };





    share|improve this answer




























      1














      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;


      };





      share|improve this answer


























        1












        1








        1






        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;


        };





        share|improve this answer














        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;


        };






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 15:30

























        answered Nov 23 '18 at 1:53









        user4581301

        19.5k51831




        19.5k51831






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

            Berounka

            I want to find a topological embedding $f : X rightarrow Y$ and $g: Y rightarrow X$, yet $X$ is not...