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

Thread: A few questions

  1. #1
    Join Date
    Jan 2001
    Posts
    2

    A few questions

    Could someone answer the following questions for me :

    1. Discuss this C++ code fragment. Do you see any potential problems ?

    BSTR bsValue = _bstr_t("a string");
    pComInterface->DoSomething(bsValue);


    2. Give examples of the problems inherent in using the C preprocessor. When
    might you want to use it ?


    3. What are the errors in this C++ code fragment ? Consider both errors that
    would prevent succesful compilation and errors that would lead to unexpected
    behaviour when using these classes.

    class base
    {
    base() {m_pData = new Data[100];}
    ~base() { delete m_pData;}
    private:
    Data* m_pData;
    }

    class derived : public base
    {
    public:
    derived() {m_iCount = 7; m_pDerivedData = new Data();}
    ~derived() { delete m_pDerivedData;}
    private:
    int m_iCount;
    Data* m_pDerivedData;
    };


    4. Discuss how you might improve this C++ code

    bool LessThan(Thing* pLeft, Thing* pRight)
    {
    // body of comparison function
    }

    typedef bool (*LessThanFnPtr)(Thing* pLeft, Thing* pRight);

    void Sort(Thing* pArray, int iSize, LessThanFnPtr pLessThan)
    {
    // bubble sort code using pLessThan to compare things
    }

    void DoSorting()
    {
    int iSize = 1000;
    Thing* pData = new Thing[iSize];

    // read in data

    Sort(pData, iSize, &LessThan);

    // use sorted data
    }


    5. How would you improve the design of this C++ code? Point out any
    problems and the causes, and suggest or show a remedy.(Hint: errors)

    void SomeClass::SetValues()
    {
    Calculator* pCalculator = new Calculator();
    m_iVal1 = pCalculator->DoCalcSomething(1, 2);
    m_iVal2 = pCalculator->DoAnotherThing(m_iVal1);
    delete pCalculator;
    }


    6. The following C++ code is correct, but it's considered to be bad style.
    Discuss.

    // Somewhere we have
    bool bCondition(false);

    // Somewhere else, perhaps immediately after...
    int iValue;
    if (bCondition)
    iValue = 1;
    else
    iValue = 2;

  2. #2
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238

    Re: A few questions

    Quote Originally Posted by tridion
    Could someone answer the following questions for me :
    FYI, we don't do other people's homework.
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: A few questions

    Code:
    3. What are the errors in this C++ code fragment ? Consider both errors that 
    would prevent succesful compilation and errors that would lead to unexpected 
    behaviour when using these classes.
    
    class base
    {
    base() {m_pData = new Data[100];}
    ~base() { delete m_pData;}
    private:
    Data* m_pData;
    }
    
    class derived : public base
    {
    public:
    derived() {m_iCount = 7; m_pDerivedData = new Data();}
    ~derived() { delete m_pDerivedData;}
    private:
    int m_iCount;
    Data* m_pDerivedData;
    };
    In base it should be:
    Code:
    ~base() { delete [] m_pData;}
    using delete instead of delete [] in this case may lead to unexpected behaviour.

    Furthermore, base constructors and destructors should be public, and the destructor virtual.

    Most of your questions are too vague to be answered (as the 5th for example).
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: A few questions

    Quote Originally Posted by tridion
    Could someone answer the following questions for me :
    Yes...I could....however, not I should answer these question but you should...so, why don't you post your answers to them and we will check whether they are correct?

  5. #5
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238

    Re: A few questions

    The thing is tridion, if you were to have shown that you have done atleast some work, most people would have been more than glad to try to explain some of the concepts and ideas to you (or whatever that is you don't understand.)
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  6. #6
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729

    Re: A few questions

    For 5, you'd probably wanna get rid of using new to allocate Calculator.

    For 4, you'd probably want to use a functor rather then a function pointer.

    For 6, you'd probably want to initialize iValue first with some default value, and then, if necessary, change it later on in the program.
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

  7. #7
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: A few questions

    Quote Originally Posted by cma
    For 4, you'd probably want to use a functor rather then a function pointer.
    What the functor is? I always thought this word means "pointer to function"... There are also those "functors" from STL <functional> though.
    "Programs must be written for people to read, and only incidentally for machines to execute."

  8. #8
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: A few questions

    Quote Originally Posted by RoboTact
    What the functor is? I always thought this word means "pointer to function"... There are also those "functors" from STL <functional> though.
    "Functors" are usually understood as being function-objects. That is, objects of classes which have their operator () overloaded.
    Insert entertaining phrase here

  9. #9
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: A few questions

    "Functors" are usually understood as being function-objects. That is, objects of classes which have their operator () overloaded.
    Actually, the term can mean anything that can be used with function notation. This includes function objects, function pointers and function names.
    Old Unix programmers never die, they just mv to /dev/null

  10. #10
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: A few questions

    Quote Originally Posted by cma
    For 5, you'd probably wanna get rid of using new to allocate Calculator.
    Why? Please explain.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  11. #11
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: A few questions

    Quote Originally Posted by cilu
    Why? Please explain.
    Because the calculator is a local variable, and thus dynamic allocation is not needed. The stack would suffice in that example. (And be exception safe as well,)
    Insert entertaining phrase here

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: A few questions

    Quote Originally Posted by tridion
    Could someone answer the following questions for me :

    1. Discuss this C++ code fragment. Do you see any potential problems ?

    BSTR bsValue = _bstr_t("a string");
    pComInterface->DoSomething(bsValue);
    Yes, I see problems.

    1) no definition of what BSTR is.
    2) _bstr_t is not a standard C++ function
    3) pComInterface is not defined.
    4) The prototype for DoSomething is not given.

    You assume that C++ means Windows, and that is not the case. A UNIX C++ programmer will more than likely not know what in the world you're talking about when you mention BSTR, _bstr_t .... Therefore the problem is the question you asked.
    3. What are the errors in this C++ code fragment ? Consider both errors that would prevent succesful compilation and errors that would lead to unexpected behaviour when using these classes.
    In addition to what others have pointed out with the problem with the wrong form of "delete", the base class does not follow the "rule of three". Please look up that answer for yourself.

    Regards,

    Paul McKenzie

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured