DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2007
    Posts
    2

    Changing scope of object

    I have two classes, call them class1 and class2. In my main(), I declare a new class object:

    class2 stepping_action = new class2;

    stepping_action has a variable called "contained" that is a boolean.

    What I need to do is change the value of this variable from within class1. When I try, it tells me that stepping_action is not in the right scope. I have tried declaring stepping_action outside of the main but I still get the same error.

    How can I change this variable from class1?

  2. #2
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Well there are several possible errors you have here:
    class2 x = new class1;
    did you mean: ???
    Code:
    class2* x = new class1;
    And this code only will work, if class1 is derived from class2.
    Changing a value in a base class is only possible if the member is public or protected. Both is considered bad practice and best avoided. You can provide public/protected getter/setters, though, with the help of which you can do the intended changes.

    Code:
    class Base
    {
        bool m_bool;
    public:
       void set(bool b)
       {
          m_bool = b;
       }
       bool get()
       {
          return m_bool;
       }
    };
    
    class Derived : public Base
    {
    public:
        void someFunc()
        {
            set(false); // this sets the value of the Base class to false
        }
    };
    
    /// in your main
    Base* pBase = new Derived;
    pBase->someFunc(); // call the set method of Base indirectly though someFunc
    Is that what you need? If not, probably you postsome of you code, indicating where your error occurs.

    Cheers,
    D
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

  3. #3
    Join Date
    Jun 2007
    Posts
    2
    I fixed the problem. Thanks!

Similar Threads

  1. Replies: 0
    Last Post: 05-28-2002, 03:25 PM
  2. Replies: 0
    Last Post: 03-18-2002, 10:31 AM
  3. Re: Writer's Uninformed Statements
    By Rich in forum .NET
    Replies: 38
    Last Post: 09-07-2001, 05:55 PM
  4. not quite off-topic
    By Jay Robinson in forum authorevents.patrick
    Replies: 1
    Last Post: 09-05-2000, 05:35 PM
  5. Re: COM object Password Security
    By Tom Shreve in forum Enterprise
    Replies: 0
    Last Post: 04-07-2000, 08:15 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links