C++ STRING

PREVIOUS

STRINGS
    
Q: What happens if you write this code?string& foo()
{
return "Hello World";
}
cout << foo() << endl;
A: 1. Will give an error since Hello World is created as a unnamed character pointer to const. it
is being assigned to non-const reference which is not allowed.
could not convert `"Hello World"' to `std::string&'
2. const string& foo1()
{
return "Hello World";
}
Gives a warning, since you are returning a reference to temporary, which will die immediately
when the expression is completed.
classsize.C:7: warning: returning reference to temporary
output : Aborted. Segment fault.
3. Char *foo1()
{
return “Hello World”;
}
Returning the address of character literal which is created on the static memory.
In C++, the compiler allows the use of string literals to initialize character arrays. A string literal
consists of zero or more characters surrounded by double quotation marks ("). A string literal
represents a sequence of characters that, taken together, form a null-terminated string. The
compiler creates static storage space for the string, null-terminates it, and puts the address of this
space into the char* variable. The type of a literal string is an array of const chars.
char* szMyString = "Hello world.";
szMyString[3] = 'q'; // undefined, modifying static buffer!!!
In the following example, the compiler automatically puts a null-character at the end of the literal
string of characters "Hello world". It then creates a storage space for the resulting string - this is
an array of const chars. Then it puts the starting address of this array into the szMyString
variable. We will try to modify this string (wherever it is stored) by accessing it via an index into
szMyString. This is a Bad Thing; the standard does not say where the compiler puts literal
strings. They can go anywhere, possibly in some place in memory that you shouldn't be
modifying.

Q: How do I convert an integer to a string?A: The simplest way is to use a stringstream:
#include
#include
#include
using namespace std;
string itos(int i) // convert int to string
{
stringstream s;
s << i;
return s.str();
}
int main()
{
int i = 127;
string ss = itos(i);
const char* p = ss.c_str();
cout << ss << " " << p << "\n";
}
Naturally, this technique works for converting any type that you can output using << to a string.

      
Q: How do you link a C++ program to C functions? A: By using the extern "C" linkage specification around the C function declarations.
Programmers should know about mangled function names and type-safe linkages. Then they
should explain how the extern "C" linkage specification statement turns that feature off
during compilation so that the linker properly links function calls to C functions.

Q: Is there anything you can do in C++ that you cannot do in C? A: No. There is nothing you can do in C++ that you cannot do in C. After all you can write a
C++ compiler in C

Q: What are the differences between a struct in C and in C++?A: In C++ a struct is similar to a class except for the default access specifier( refere to other
question in the document). In C we have to include the struct keyword when declaring
struct. In c++ we don’t have to.

Q: What does extern "C" int func(int *, Foo) accomplish?A: It will turn o_ "name mangling" for func so that one can link to code compiled by a C
compiler.

Q: What are the access privileges in C++? What is the default access level?A: The access privileges in C++ are private, public and protected. The default access level
assigned to members of a class is private. Private members of a class are accessible only within
the class and by friends of the class. Protected members are accessible by the class itself and it's
sub-classes. Public members of a class can be accessed by anyone.

Q: How does C++ help with the tradeoff of safety vs. usability?A: In C, encapsulation was accomplished by making things static in a compilation unit or
module. This prevented another module from accessing the static stuff. (By the way, static data
at file-scope is now deprecated in C++: don't do that.)
Unfortunately this approach doesn't support multiple instances of the data, since there is no direct
support for making multiple instances of a module's static data. If multiple instances were needed
in C, programmers typically used a struct. But unfortunately C structs don't support
encapsulation. This exacerbates the tradeoff between safety (information hiding) and usability
(multiple instances).
In C++, you can have both multiple instances and encapsulation via a class. The public part of a
class contains the class's interface, which normally consists of the class's public member
functions and its friend functions. The private and/or protected parts of a class contain the class's
implementation, which is typically where the data lives.
The end result is like an "encapsulated struct." This reduces the tradeoff between safety
(information hiding) and usability (multiple instances).

PREVIOUS

No comments:

Post a Comment