dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: What is the difference between these two statements?

  1. #1
    Join Date
    Sep 2018
    Posts
    4

    Question What is the difference between these two statements?

    I was working on a homework assignment when I came across two methods to get a certain job done.
    I am not going to write whole class code here. Actually I am doing aggregation practice.

    so can anybody explain me the difference between these two approaches?

    Approach # 1:
    Function in class =

    Code:
    addCourse(Course* c)
    {
        cptr[1]=c
    }
    In Main call=

    Code:
    addCourse(&c1);
    Approach # 2:
    Function in class=

    Code:
    addCourse(Course c)
    {
        cptr[1]=&c
    }
    In Main call=

    Code:
    addCourse(c1);
    ----------------------------------
    Considering that getID is a getter function and id is an int value
    cptr[1].getID==id
    above statement works fine when approach # 1 is used and it gets skipped when using the other approach..
    Whats the matter?
    Last edited by 2kaud; November 21st, 2018 at 11:05 AM. Reason: Added code tags

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,515

    Re: What is the difference between these two statements?

    In approach #1, you are passing as a param for addCouse() a pointer to type Course as c. In the call statement you are obtaining the address of c1 and pasing this as the param. Thus in addCouse(), c is the address of c1 in Main(). Everything is ok and as you're found, works as expected.

    However in approach #2, things are slightly different. Here the param to addCouse is of type Course - not a pointer (or a reference). Hence the data is 'passed by value' which means that in addCouse(), c is a copy of the variable c1 in Main() - not the same variable. As it's a different variable, it has a different address. Hence in addCouse(), &c is the address of the c variable used in addCouse() - which is a different address to that of c1 in main(). The variable c only exists until the end of the function addCouse() - so &c is only valid during addCouse() and not outside it. So in main(), cptr[] now has an invalid pointer.

    To 'correct' approach #2, c needs to be passed 'by ref' so that address of c in addCourse() is the same as that of the argument c1 in main().

    Code:
    addCourse(Course& c)
    {
        cptr[1] = &c;
    }
    Last edited by 2kaud; November 21st, 2018 at 11:08 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.4)

  3. #3
    Join Date
    Sep 2018
    Posts
    4

    Re: What is the difference between these two statements?

    I tried to correct the approach 2 the same way you did but the addCourse got red underline showing the following error
    1 IntelliSense: declaration is incompatible with "bool Student::addCourse(Course *c)" (declared at line 14 of "c:\users\najam ul saqib\documents\visual studio 2013\projects\course student\course student\StudentHeader.h") c:\Users\Najam Ul Saqib\Documents\Visual Studio 2013\Projects\Course Student\Course Student\StudentEnc.cpp 28 15 Course Student

    Quote Originally Posted by 2kaud View Post
    In approach #1, you are passing as a param for addCouse() a pointer to type Course as c. In the call statement you are obtaining the address of c1 and pasing this as the param. Thus in addCouse(), c is the address of c1 in Main(). Everything is ok and as you're found, works as expected.

    However in approach #2, things are slightly different. Here the param to addCouse is of type Course - not a pointer (or a reference). Hence the data is 'passed by value' which means that in addCouse(), c is a copy of the variable c1 in Main() - not the same variable. As it's a different variable, it has a different address. Hence in addCouse(), &c is the address of the c variable used in addCouse() - which is a different address to that of c1 in main(). The variable c only exists until the end of the function addCouse() - so &c is only valid during addCouse() and not outside it. So in main(), cptr[] now has an invalid pointer.

    To 'correct' approach #2, c needs to be passed 'by ref' so that address of c in addCourse() is the same as that of the argument c1 in main().

    Code:
    addCourse(Course& c)
    {
        cptr[1] = &c;
    }

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,515

    Re: What is the difference between these two statements?

    Yes, you will need to change the declaration of addCourse() in the class Student to match the revised function definition. The function declaration and definition needs to have the same interface specification.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.4)

  5. #5
    Join Date
    Sep 2018
    Posts
    4

    Re: What is the difference between these two statements?

    Quote Originally Posted by 2kaud View Post
    Yes, you will need to change the declaration of addCourse() in the class Student to match the revised function definition. The function declaration and definition needs to have the same interface specification.
    Thanks a lot mate!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)