Variable to be constant after 1 input & return

Hello,

I programm an account programm which stores name and password for a new account in accordance to different rules.

1. Question on constant variable
I first get the user input (see code below) and then transfer the data from a public to a private variable within my class. For my private variable name_, I would like it to get constant after copying the n_ value. So from this point in my programm, it shall be constant, but of course I require the first input. If I use "const" my programm does not allow any change to name_. Is there any possibility? I would expect this kind of programming to be a general approach, so that variables are constant from a certain point on.
It might be that pointer are a solution, but I do not understand them well until now.

2. Return
I would like to have the getName_() method in my class only for the function to get the name. To check if it is correct, I want to create a checkName() method. Until now I have the complete function in one method which works.
I now try to pass the name_ variable from my getName() to the checkName() which are both inside the class on the same level.
I tried it to declare the functions as friend and to "return name_" (which is a CString). Both do not work. In general I have the problem that 2 functions within the same class do not know each other.
Is there any general approach?

1
2
3
4
5
6
7
8
9
10
...
Account::getName_()
{
    Acc_Popup: cout << "Please type in your new account; no space is allowed" << endl;
    getline(std::cin,n_); // n_ is public variable
    name_ = n_; //name is private variable

/*... Thereafter I perform certain checks. If true I write the name_ to a file,
if false I perform an error with try, throw, catch. */
Last edited on
Something along these lines, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>

struct account
{
    explicit account( const std::string& name ) : name_( valid_name(name) ) {}

    const std::string name_ ;

    private:

        // return str if it contains a valid name; otherwise throw
        static const std::string& valid_name( const std::string& str )
        {
            // if not valid, throw etc.

            return str ;
        }
};

account create_account()
{
    std::string name ;
    std::cout << "account name? " ;
    std::getline( std::cin, name ) ;

    return account{ name } ; // may throw if the name is invalid
}

int main()
{
    const account ac = create_account() ;
    // ...
}
Sorry, I tried every combination, also applying the structure above. It is for me unclear how to separate the header and source.

With the code:
"explicit account( const std::string& name ) : name_( valid_name(name) ) {}"
in the header I always get the fault:
"Only constructors can take member initialisations"
and many other "only constructors ..." failure messages.
So my header does not expect this to be a constructor although it should be one.

Could someone please show how to separate header and source?
> It is for me unclear how to separate the header and source.

Header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// #include guard
#include <iostream>
#include <string>

struct account
{
    explicit account( const std::string& name )  ;

    const std::string name_ ;

    private:

        // return str if it contains a valid name; otherwise throw
        static const std::string& valid_name( const std::string& str ) ;
 };


Implementation (.cpp)
1
2
3
4
5
6
7
8
9
10
// #include header

account::account( const std::string& name ) : name_( valid_name(name) ) {}

const std::string& account::valid_name( const std::string& str )
{
    // if not valid, throw etc.

    return str ;
}



> in the header I always get the fault:
> "Only constructors can take member initialisations"
> and many other "only constructors ..." failure messages.

Make sure that the class-name in the constructor is spelt correctly; it is case-sensitive.
Topic archived. No new replies allowed.