A Few C++ Questions

T

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);


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;
 
V

Victor Bazarov

tridion said:
Could someone answer the following questions for me :

Homework is supposed to be done by the assignee, not by the newsgroup.
I'll start, you finish.
1. Discuss this C++ code fragment. Do you see any potential problems ?

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

Of course I see potential problems. Code fragments are based on many
assumptions, and as we know, assumptions are the mother of all FUps.
2. Give examples of the problems inherent in using the C preprocessor.
When might you want to use it ?

#define NULL 0
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];}


'Data' is undefined.
~base() { delete m_pData;}

Wrong delete is used.
private:
Data* m_pData;
}

The "Rule of Three" is not followed. A semicolon is missing.
class derived : public base

Impossible to derive from 'base' since 'base' has both c-tor and d-tor
private.
{
public:
derived() {m_iCount = 7; m_pDerivedData = new Data();}


'Data' is still undefined.
~derived() { delete m_pDerivedData;}
private:
int m_iCount;
Data* m_pDerivedData;
};

The "Rule of Three" is not followed here either.
4. Discuss how you might improve this C++ code
[...]

V
 
P

Peter Koch Larsen

Ill continue where Victor stopped ;-)
tridion said:
Could someone answer the following questions for me :
[snip]

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
}

Use the standard library. Then this comes down to:

void DoSorting()
{
std::vector<Thing> data;

// read in data
std::sort(data.begin(),data.end()); // assuming Thing is comparable
and a "normal" sort is wanted.
}
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;
}

Do not use new here:
void SomeClass::SetValues()
{
Calculator pCalculator;
m_iVal1 = pCalculator.DoCalcSomething(1, 2);
m_iVal2 = pCalculator.DoAnotherThing(m_iVal1);
}

Note how the variable naming convention now gives the calculator object a
misleading name.
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;

You could use iValue = bCondition?1:2;

but considering the horrible style found in these samples I'm not sure this
is what is meant.

/Peter
 
R

red floyd

tridion said:
Could someone answer the following questions for me :
[blatant "do my homework" request redacted]

Could you give me your instructor's email address so I can send it
direct to him or her, saving you the trouble?
 
M

Mike Wahler

Peter Koch Larsen said:
You could use iValue = bCondition?1:2;

but considering the horrible style found in these samples I'm not sure this
is what is meant.

In further pursuit of 'horrible', how about:

int iValue(-bCondition + 2);

:)

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top