Thread: Help Needed with Tricky Question

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,318
    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.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    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.
    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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;
    }

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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!
    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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 .

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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,149
    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, 12:58 PM
  5. A tricky macro
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 07:04 PM