Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6631957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:37:09+00:00 2026-05-25T22:37:09+00:00

I am currently working on a widget-based graphical user interface. It is structured as

  • 0

I am currently working on a widget-based graphical user interface. It is structured as a tree with Widgets as the leaves and Containers as the nodes of the tree. The (solvable) problem with this structure is that the Widget-class takes a reference to the Container that is its parent. However, this makes it impossible for the Container class to access the protected members of the Widget-class (here the “draw” member is causing trouble).

Here is the core of the code causing the problem. Of course this can be solved by making the members public. However, that is not the style that I would like.

ClassesTest.h:

class Container;

class Widget {
public:
    Widget(Container *parent);
    virtual ~Widget();

protected:
    Container *parent;

    virtual void draw();
};

class Container : public Widget {
public:
    Container(Container *parent);
    virtual ~Container();

protected:
    std::list<Widget *> childs;

private:
    friend Widget::Widget(Container *);
    friend Widget::~Widget();

    virtual void draw();

    void addChild(Widget *child);
    void removeChild(Widget *child);
};

ClassesTest.cpp

#include "stdafx.h"
#include "ClassesTest.h"

Widget::Widget(Container *parent) {
    this->parent = parent;
    parent->addChild(this);
}

Widget::~Widget() {
    parent->removeChild(this);
}

void Widget::draw() {
    //Draw the leaf
}


Container::Container(Container *parent) : Widget(parent) {}

Container::~Container() {}

void Container::draw() {
    //Draw all the childs

    for (std::list<Widget *>::iterator i = childs.begin(); i != childs.end(); i++) {
        (*i)->draw();
    }
}

void Container::addChild(Widget *child) {
    childs.push_back(child);
}

void Container::removeChild(Widget *child) {
    childs.remove(child);
}


int main(int argc, char* argv[])
{
    //Do something useful!
    return 0;
}

And this is the output Visual Studio 2008 gives me when I am trying to compile my code:

1>------ Build started: Project: ClassesTest, Configuration: Debug Win32 ------
1>Compiling...
1>ClassesTest.cpp
1>e:\visual studio 2008\projects\classestest\classestest\classestest.cpp(26) : error C2248: 'Widget::draw' : cannot access protected member declared in class 'Widget'
1>        e:\visual studio 2008\projects\classestest\classestest\classestest.h(14) : see declaration of 'Widget::draw'
1>        e:\visual studio 2008\projects\classestest\classestest\classestest.h(6) : see declaration of 'Widget'
1>Build log was saved at "file://e:\Visual Studio 2008\Projects\ClassesTest\ClassesTest\Debug\BuildLog.htm"
1>ClassesTest - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Any suggestions would be appreciated!

Filip

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:37:10+00:00Added an answer on May 25, 2026 at 10:37 pm

    Your code is essentially equal to this, with regard to your specific problem:

    class Base
    {
    protected:
        virtual void f() {}
    };
    
    class Derived : public Base
    {
        void h()
        {
            Base().f();    // no can do - Base() creates another instance.
            f();           // sure, why not. it's the same instance, go ahead.
            Derived().f(); // sure, why not. it's the same type, go ahead.
        }
    };
    

    The problem is that although Derived inherits from Base, it still doesn’t have access to Base‘s protected members. The way access rights works here is as follows:

    1. Derived can not access Base‘s protected stuff if Base is a different instance.
    2. Derived can access Base‘s protected stuff in it’s own instance.
    3. Derived can access another Derived‘s private stuff.

    The quickest way to solve your problem would probably make Container::draw() a friend of Widget.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on a application that will be implementing user customizable widgets
Currently i am working on a plugin and widget based CMS. A widget is
Currently working in the deployment of an OFBiz based ERP, we've come to the
Cocoa newbie here. I am working on an iPhone UITableViewController-based widget that can be
An application I am currently working on uses the SWT tool-kit's Browser widget to
I'm working on a custom dojo-widget. Within the widget I am creating some DOM-Nodes
Currently working on a VBScript to automate some of the dirty PST ingestion work
I currently working on an issue tracker for my company to help them keep
Am currently working on an application that requires users to submit posts and comments
I`m currently working out the design for simple graphic editor, who support trivial operations

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.