Click to See Complete Forum and Search --> : A few questions


tridion
February 12th, 2005, 11:26 AM
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;

YourSurrogateGod
February 12th, 2005, 11:45 AM
Could someone answer the following questions for me :
FYI, we don't do other people's homework.

cilu
February 12th, 2005, 11:53 AM
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:

~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).

Andreas Masur
February 13th, 2005, 04:57 AM
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? ;)

YourSurrogateGod
February 13th, 2005, 11:16 AM
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.)

cma
February 13th, 2005, 12:13 PM
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.

RoboTact
February 13th, 2005, 12:20 PM
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.

wien
February 13th, 2005, 03:17 PM
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.

HighCommander4
February 13th, 2005, 03:29 PM
"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.

cilu
February 13th, 2005, 04:22 PM
For 5, you'd probably wanna get rid of using new to allocate Calculator.

Why? Please explain.

wien
February 13th, 2005, 04:39 PM
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,)

Paul McKenzie
February 13th, 2005, 09:47 PM
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