C++ function using strings


 
Thread Tools Search this Thread
Top Forums Programming C++ function using strings
# 1  
Old 12-12-2012
C++ function using strings

I want to create a message function to be used as follows

Code:
string s1 = "Hello";
string s2 = "World";

error_notify ("Hello", "World");
error_notify (s1, "World");
error_notify ("Hello", s2);
error_notify (s1, s2);

char* c1 = "Hello";
char* c2 = "Hello";
error_notify ("Hello", s2);
error_notify (c1, "World");

This is what I have tried but am getting lot of errors.

Code:
void  error_notify (
  const char*  brief_msg,
  const char*  detailed_msg
) {
  cerr << "\nRUNTIME ERROR!  " << brief_msg << endl << detailed_msg << endl;
}

void  error_notify (
  const string  brief_msg,
  const string  detailed_msg
) {
  cerr << "\nRUNTIME ERROR!  " << brief_msg << endl << detailed_msg << endl;
}

---------- Post updated at 05:49 PM ---------- Previous update was at 05:28 PM ----------

I am also getting confused whether to use

Code:
string
string&
const string
const string&

in the argument list

---------- Post updated at 06:16 PM ---------- Previous update was at 05:49 PM ----------

I am going to stick with

Code:
const string&

which should be ok
# 2  
Old 12-14-2012
The '&' is just a 'silent pass by pointer reference compiler directive', efficient if you want to do less copying of a large object, and potentially allowing modification of the original since the called method has access to the original, not a copy. I call it silent (and have mixed feelings about it as a direction) because, unlike the C practice of explicit pass by pointer reference, this one is invisible when calls are instantiated unless you refer to the function/method declaration. Saving the programmer keystrokes and encouraging pass by reference but also improving the chances of programmer error.

Modifying the original passed by reference is a good segue into const, with is more often used in call declarations to say the called method cannot directly modify the original. Of course, if the called method can call a class method that modifies, that gets a bit thin -- it was more for primitives -- look but do not change. Using const on a declaration says that the initialized value is the only value, it can never be changed, so it can be copied freely, cached without worrying about having a stale copy, a bit like final, but once that was just JAVA. Java's final vs. C++'s const - Stack Overflow

Const can have either of two meanings with pointers, const pointer or pointer to const data: C++ Reference - Const Pointers - Cprogramming.com

Quick interview question -- what are the three meanings of static?
# 3  
Old 01-09-2013
Quote:
This is what I have tried but am getting lot of errors.
Which errors?
# 4  
Old 01-09-2013
To begin with, you should consider this an error:
Code:
char* c1 = "Hello";

You're creating a mutable pointer into a constant memory block. It should be:
Code:
const char* c1 = "Hello";

When passing object in a read-only capacity, you should consider passing by const reference rather than copying. An object passed this way shows up as const, and you can only call const methods on the object.

A string can be constructed from a const char*.

Using the notion of const-correctness, and temporary string can be constructed from a const char*, you get:
Code:
void error_notify(const std::string &brief_msg, const std::string &detailed_msg)
{
	std::cerr << "\nRUNTIME ERROR!  " << brief_msg << ':' << detailed_msg << std::endl;
}

This User Gave Thanks to kbw For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

3. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies

4. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

5. UNIX for Dummies Questions & Answers

Issue when using egrep to extract strings (too many strings)

Dear all, I have a data like below (n of rows=400,000) and I want to extract the rows with certain strings. I use code below. It works if there is not too many strings for example n of strings <5000. while I have 90,000 strings to extract. If I use the egrep code below, I will get error: ... (3 Replies)
Discussion started by: forevertl
3 Replies

6. Shell Programming and Scripting

Bash function accepting list of strings and error message

I have a variable strLst containing a list of strings. I want to create a function that takes the list and an error message. If the number of strings is 0 or greater than 1, I print the error message. (1 Reply)
Discussion started by: kristinu
1 Replies

7. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

8. UNIX for Dummies Questions & Answers

Delete strings in file1 based on the list of strings in file2

Hello guys, should be a very easy questn for you: I need to delete strings in file1 based on the list of strings in file2. like file2: word1_word2_ word3_word5_ word3_word4_ word6_word7_ file1: word1_word2_otherwords..,word3_word5_others... (7 Replies)
Discussion started by: roussine
7 Replies

9. Shell Programming and Scripting

Return an array of strings from user defined function in awk

Hello Friends, Is it possible to return an array from a user defined function in awk ? example: gawk ' BEGIN{} { catch_line = my_function(i) print catch_line print catch_line print catch_line } function my_function(i) { print "echo" line= "awk" line= "gawk"... (2 Replies)
Discussion started by: user_prady
2 Replies

10. Shell Programming and Scripting

How to concatenate two strings or several strings into one string in B-shell?

like connect "summer" and "winter" to "summerwinter"? Can anybody help me? thanks a lot. (2 Replies)
Discussion started by: fontana
2 Replies
Login or Register to Ask a Question