Thread: How to refer an int type

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    11

    How to refer an int type

    I have one class A which has an "int" field b; I defined another int type variable c. When I make any changes to A.b, I want c changes accordingly such that c=A.b .

    One can always add a line c=A.b whenever A.b changes. But is there any better way to do that such as the method used in C/C++ : we can let c be a reference of A.b? (eg.: c=&(A.b) in c++)

    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You can, but at that point you should stop pretending that you're using C++.

    Having a pointer (c) which is outside your class, pointing directly at a data member of the class (A.b) flies directly in the face of one of the primary reasons for creating C++ in the first place. Namely that one of the problems of writing larger systems was people forever meddling in an uncontrolled manner in other peoples data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    11
    Hi salem, that makes sense. Actually, I don't want to change the value A.b outside A bypassing A's public property or methods. What I want is letting c be a read-only alias of A.b.
    Code:
    public class B
    {
        private List<int> A;
        public int c;
    }
    I want that c always equals to A.Count, thus I can use B.c instead of B.A.c . I am thinking if I have a smart way to avoid adding c=A.Count everywhere in class B because I change A's contents frequently in B. Any ideas?
    Last edited by igoogleu; 09-21-2008 at 09:58 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What's "frequent" ?
    A million times a second?

    If you write your method as an inline function, the compiler will likely optimise away any call overhead and reading the integer will be a single instruction anyway, without the need to resort to interface-busting trick accesses. This is commonly called "premature optimisation disease".

    First rule is write clear simple code which does what you want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    If I'm understanding correctly, I guess you could create an event that gets set in the mutator of class A's b field, subscribe to the event and change c accordingly?

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    11
    Quote Originally Posted by Salem View Post
    What's "frequent" ?
    A million times a second?

    If you write your method as an inline function, the compiler will likely optimise away any call overhead and reading the integer will be a single instruction anyway, without the need to resort to interface-busting trick accesses. This is commonly called "premature optimisation disease".

    First rule is write clear simple code which does what you want.
    well, by "frequent" I mean I will manipulate A in many methods of B now and in the future. Thus each time I write a method manipulating A, I have to add a line "c=A.b". If there is a way I can write just one line which links c to A.b, thus I can access c outside B in a read-only manner, and save some duplicated code lines ("c=A.b") and avoid some trouble in case of forgetting to add c=A.b.

    That has nothing to do with the inline function to save some parameter stacking time.

    Thanks.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    in your example with List<int>, you can just write a property to return the value of .Count

    i'm still not really clear what you're trying to do though. B sounds like a custom class. but can you explain in more detail with A and c's types are?
    Last edited by bling; 09-21-2008 at 12:30 PM.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by igoogleu View Post
    Code:
    public class B
    {
        private List<int> A;
        public int c;
    }
    I don't know what language that is, but is isn't C++.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    11
    Quote Originally Posted by bling View Post
    in your example with List<int>, you can just write a property to return the value of .Count

    i'm still not really clear what you're trying to do though. B sounds like a custom class. but can you explain in more detail with A and c's types are?
    Currently I wrote a property "c" to return the value of .Count, but I have to add c= .Count everywhere .Count changes, right?

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    11
    Quote Originally Posted by cpjust View Post
    I don't know what language that is, but is isn't C++.
    I don't know what was Mod doing? I posted it in C# section, and I am asking a C# question. Why move it to C++ section?

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    What you're looking for is called a property. Syntactically it's like using a variable though technically it's still a function (method) call.
    Code:
    public class MyClass
    {
      public int c
      {
        get
        {
          return A.c;
        }
      }
    
      private List<int> A;
    }
    And use it as such:
    Code:
    MyClass m = new MyClass();
    int NrOfItemsInList = m.c;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    11
    Quote Originally Posted by Magos View Post
    What you're looking for is called a property. Syntactically it's like using a variable though technically it's still a function (method) call.
    Code:
    public class MyClass
    {
      public int c
      {
        get
        {
          return A.c;
        }
      }
    
      private List<int> A;
    }
    And use it as such:
    Code:
    MyClass m = new MyClass();
    int NrOfItemsInList = m.c;
    Thanks! That's what I did last night! I was thinking too much and forgot the simplest way. I don't need a reference to A.c if I just use property like that.

  13. #13
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    "return A.c" should be "return A.Count", but I guess you figured that out
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM