- Joined
- 5/27/09
- Messages
- 106
- Points
- 28
1). What is the difference between malloc and new? Why is one preferable to the other?
2). memset is sometimes used to initialize data in a constructor like the example below. What is the benefit of initializing this way? Does it work in this example? Does it work in general ? Is it a good idea in general?
3). You have a class that many libraries depend on. Now you need to modify the class for one application. Which of the following changes require recompiling all libraries before it is safe to build the application?
a. add a constructor
b. add a data member
c. change destructor into virtual
d. add an argument with default value to an existing member function
Let's see some answers...
2). memset is sometimes used to initialize data in a constructor like the example below. What is the benefit of initializing this way? Does it work in this example? Does it work in general ? Is it a good idea in general?
C++:
[/SIZE]
[SIZE=3] class A {[/SIZE]
[SIZE=3] public:[/SIZE]
[SIZE=3] A();[/SIZE]
[SIZE=3] private:[/SIZE]
[SIZE=3] int a;[/SIZE]
[SIZE=3] float f;[/SIZE]
[SIZE=3] char str[35];[/SIZE]
[SIZE=3] long *lp;[/SIZE]
[SIZE=3] };[/SIZE]
[SIZE=3] [/SIZE]
[SIZE=3] A::A()[/SIZE]
[SIZE=3] {[/SIZE]
[SIZE=3] memset(this, 0, sizeof(*this));[/SIZE]
[SIZE=3] }[/SIZE]
[SIZE=3]
3). You have a class that many libraries depend on. Now you need to modify the class for one application. Which of the following changes require recompiling all libraries before it is safe to build the application?
a. add a constructor
b. add a data member
c. change destructor into virtual
d. add an argument with default value to an existing member function
Let's see some answers...