Like Tree4Likes
  • 1 Post By laserlight
  • 1 Post By iMalc
  • 1 Post By King Mir
  • 1 Post By Elysia

Help Needed with Tricky Question

This is a discussion on Help Needed with Tricky Question within the C++ Programming forums, part of the General Programming Boards category; Below is my code. What i want to do is to assign a value to variable data. I'm not allowed ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    Help Needed with Tricky Question

    Below is my code. What i want to do is to assign a value to variable data. I'm not allowed to write any method in the public area of XYZ class. But a pointer to an object of XYZ class is allowed but in the private area of XYZ class only but public portion of ABC class is allowed. I can write the methods into this.

    Any idea to do this...???

    Code:
    class ABC 
    {   
    private:         
         class XYZ        
         {           
          private:                
              int data;        
         };    
    
    public:        
        ABC(); 
    };
    Regards

    Lucky

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by luckyali444
    Any idea to do this...???
    Why do you want to do this? If this is for an interview question, I suggest that you move on, because a company that expects its developers to know how to cheat the access control mechanism intended to help developers is probably not worth working for.
    Elysia likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    Quote Originally Posted by laserlight View Post
    Why do you want to do this? If this is for an interview question, I suggest that you move on, because a company that expects its developers to know how to cheat the access control mechanism intended to help developers is probably not worth working for.
    thanks for your reply Laserlight, i am not going to do any kind of job. I am a student and one of my friend asked me that if it is possible to assign any value to data but without making any methods in Public part of XYZ class.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by luckyali444
    I am a student and one of my friend asked me that if it is possible to assign any value to data but without making any methods in Public part of XYZ class.
    No, it is not possible. However, it is possible to make it appear as if you have assigned a value to that private member variable, though what you really would be doing is just writing to the memory that is used by the member variable, by making assumptions about the member layout of the object. This should not be done if the object is not of a POD type (a class with private members is not a POD type).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    I think perhaps the trick is that friends can help you do this .
    Well at least it doesn't violate the rules laid out so far.
    manasij7479 likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by iMalc
    Well at least it doesn't violate the rules laid out so far.
    Well, to exploit the letter of the rule, we can define a non-virtual public member function in XYZ, since that would not be a method, in C++ terminology.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,045
    Actually, in C++11 XYZ would be POD. Data member don't have to be public, they just have to have the same access level.
    laserlight likes this.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Here is a *dangerous* experiment I once tried:
    (Feel free to delete this post if considered harmful )
    Anyway.. here it is..
    Code:
    #include<iostream>
    class foo1
    {
    	int x;
    public:
    	foo1(int x_){x=x_;};
    };
    class foo2
    {
    public:
    	int x;
    };
    
    
    int main()
    {
    
    
    	foo1* f1 = new foo1(5);
    	foo2* f2;
    	f2= (foo2*)f1;  //Shooting downwards, hoping not to hurt the legs !
    	
    	std::cout<<f2->x;
    	return 0;
    }
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,662
    That leaks memory, and you don't need to do that.
    This should suffice:

    Code:
        foo1 f1(5);
        foo2* f2 = reinterpret_cast<foo2*>(&f1);  //Shooting downwards, hoping not to hurt the legs !
    Of course, don't use this in production code. This is for experimental purposes only!
    manasij7479 likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Of course, don't use this in production code. This is for experimental purposes only!
    Unless you're aiming for this.

    Anyway, my point&& question was whether the integer is guaranteed to remain in the same position for the two classes .
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,662
    If they are compiled with the same compiler and the same padding settings, then it is likely. However, it is still undefined. Any attempt to circumvent access control will result in undefined behavior AFAIK.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,045
    The standard says "A pointer to a standard-layout struct object, suitably converted using a reinterpret_cast, points to its initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa. [ Note: There might therefore be unnamed padding within a standard-layout struct object, but not at its beginning,
    as necessary to achieve appropriate alignment. —end note ]"

    So reinterpret_cast<int*>(&xyz) == &xyz.data.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c tricky code
    By theju112 in forum C Programming
    Replies: 6
    Last Post: 08-12-2011, 05:34 PM
  2. Tricky C
    By ronrardin in forum C Programming
    Replies: 2
    Last Post: 09-29-2010, 01:01 PM
  3. A tricky(maybe simple) question
    By wayne08 in forum C++ Programming
    Replies: 9
    Last Post: 04-20-2010, 09:35 AM
  4. Tricky C Question
    By ganesh bala in forum C Programming
    Replies: 11
    Last Post: 01-28-2009, 11:58 AM
  5. A tricky macro
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 07:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21