Thread: Member never assigned in sample code!!!

  1. #1
    Registered User
    Join Date
    Jun 2010

    Member never assigned in sample code!!!


    I am trying to learn a little about inheritance in C through an article I found on the web at:

    Inheritance and Polymorphism in C - CodeProject

    There is one thing I don't understand. In this article, there are two structures... a base struct called "Person" and a derived struct called "employee". The intent is to be able to fetch functions in the employee module via either a person pointer type or an employee pointer type. Also, either object can have access to any data members via two linking members ... pDerivedObj* and pBaseObj*. Atleast this is what I think. Please correct me if I am wrong. My concern is the following.

    If you go to p. 6 of 8, under the title "Structure of the employee object", we see a diagram of the two strucutres side by side. Both structures have a member pointing to the other structure. So in other words, the Person struct has pDerivedObj pointing to the employee object and the employee struct has pBaseObj pointing to the person object.

    The pDerivedObj is assigned the address of the employee object at the following line of the new_Emplyee() function:

    pObj->pDerivedObj = pEmpObj;

    But what I don't understand is that I don't see where the addresss of the person object is assigned to the pBaseObj pointer member of the employee object??? The diagram shows the arrow going from the pBasePObj* to the person struct, but I don't see where the code does this assignment.

    Can someone please help me, I have looked over the code in this article many times and I still just don't see it.

    All help appreciated.



  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    If you assign some value to an instance of your base object, and you are pointing at that base object, then you can use whatever was assigned through the pointer you have.
    struct foo
       type something;
    struct bar
        struct foo *baz;
    struct foo somefoo;
    struct bar somebar;
    somefoo.baz = &somefoo;
    somefoo.baz->something = blah;
    I didn't look through your example, but that's basically how it would work. If they actually assigned values to 'somefoo', then 'somebar' can access them via its pointer to that.

    Hope is the first step on the road to disappointment.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    The diagram is question is simply trying to explain how virtual dispatch works. It's under the covers, and might not even work in the way implied by the diagram. Unless you're interested in how C++ actually works under the hood, you don't need to worry about this.

    The short answer is "the compiler does it."

    EDIT: Whoo! I get the "completely missed the point of the question" award for today. Sorry.
    Last edited by brewbuck; 02-15-2011 at 06:11 PM.
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

  4. #4
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    Looks like the author just forgot that line of code, along with freeing up allocated memory if allocating space for a department or company string fails causing new_Employee to fail. I think it's for proof of concept, not for you to use in production.

    EDIT: That was an interesting read by the way. I've messed around with some of these concepts before and found them fascinating study, if not very practical. Good way to learn some tougher concepts in C and get an idea of how some OO languages do it under the hood.
    Last edited by anduril462; 02-15-2011 at 05:56 PM.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Hi Quzah,

    Thanks for your sample. I have to admit, that I am trying to understand what is under the hood of OOP when using inheritance mixed with polymorphism as shown in my article link. This is so I can try to apply these principles in C.

    But I am having trouble understanding why everyone is saying that OOP is so important.

    So if you don't mind me asking you a few silly questions.
    1)Your "struct bar" is it the actual base class?
    2)Instead of your "baz*", I could replace it with pointer functions right?
    3) For your example to really illustrate inheritance and polymorphism, would it be more complete if you had more than one foo? For example like a "struct bar" as the base class , a "struct footype1", and a struct footype2" as derived classes?

    Please do get back I am really confused on this subject!

    Thanks all who replied!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Illegal Instruction at perfectly fine Code
    By m00ni in forum C Programming
    Replies: 24
    Last Post: 02-14-2011, 02:56 AM
  2. Pre-Interviewer asking sample code
    By jayee_spicyguy in forum C Programming
    Replies: 3
    Last Post: 02-09-2009, 11:21 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM