|
-
February 12th, 2005, 12:26 PM
#1
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;
-
February 12th, 2005, 12:45 PM
#2
Re: A few questions
 Originally Posted by tridion
Could someone answer the following questions for me :
FYI, we don't do other people's homework.
-
February 12th, 2005, 12:53 PM
#3
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).
-
February 13th, 2005, 05:57 AM
#4
Re: A few questions
 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?
-
February 13th, 2005, 12:16 PM
#5
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.)
-
February 13th, 2005, 01:13 PM
#6
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
-
February 13th, 2005, 01:20 PM
#7
Re: A few questions
 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."
-
February 13th, 2005, 04:17 PM
#8
Re: A few questions
 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
-
February 13th, 2005, 04:29 PM
#9
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
-
February 13th, 2005, 05:22 PM
#10
Re: A few questions
 Originally Posted by cma
For 5, you'd probably wanna get rid of using new to allocate Calculator.
Why? Please explain.
-
February 13th, 2005, 05:39 PM
#11
Re: A few questions
 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
-
February 13th, 2005, 10:47 PM
#12
Re: A few questions
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|