Constructor parameters using new with a non-pointer

Hi there!
I've made the following code (just for fun):

main.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
Project name: java2cpp
Author: Lucas Henrique
Date (start): 30/11/2013
*/

//In this "framework", you DON'T need use "package". (And you can't)
#include "java.h" /* See one*/
importing java::basics; /*See two*/


int main(int argc, String args[]) /*See three*/
{
System.out.println("Hello World!");
System.out.print("Printing without spaces. ");
System.out.print("Text two, like Java!");
Scanner input = new Scanner("System.in");


}

/*                                              See xxx...
1. The main file root for the Java programmer that DON'T want to use an interpreter. "#include" is an C++ keyword to include function-files. Have sure that the java.h file is in the project directory
2. The "import" keyword is used by C++, so I made this.
importing java;
using basics; is :"use namespace basics from the namespace java". "import java::basics" ESSENTIAL to everything work. There are also others namespaces from namespace java. Use :: operator as . in java, but you dont need to (and you cant) use * to include (they aren't files);
3. The main ONLY return integers. By default, C++ has "argc", and this is the argument count. Example:

Source:
#include "java.h"
using java::basics;
int main(int argc, String args[])
{
System.out.println(argc);
}
String in = "Sytem.in";
On command line:
MyProject.exe argumentone argumenttwo argumentthree blabla helloworld

Return: 6

            Logic:
1. EXE name (MyProject.exe)
2. argumentone
3. argumenttwo
4. argumentthree
5. blabla
6. helloworld

Please, see README.htm file for more info.
*/

java.h
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef JAVA_H
#define JAVA_H
#define importing using namespace
#include <iostream>
#include <cstdlib>
#include <fstream>

namespace java
{
    namespace basics
    {
typedef char* String;
class System_
{
public:

String in = "System.in";

    class out_
    {
        public:

        template <typename T>
        void println(const T& var)
        {
            std::cout << var << '\n';
        }
        template <typename T>
        void print(const T& var)
        {
            std::cout << var;
        }
    } out;
} System;

class Scanner : public System_
{
    public:
        Scanner();
    Scanner(String sysType)
    {
        if (sysType=="System.in"){}
        else
        {
        std::cout<<"Error while creating Scanner to input. (ID 0001) - Invalid System type.";
        std::ofstream error;
        error.open("error.log",std::ios::app);
        error<<"\nError with the constructor java::basics::Scanner::Scanner(). Process returned 1, so the entered sysType is INVALID (note: \"System.in\" is aceptable (Scanner is used for input values)\n";
        error.close();
        exit(1);
        }
    }
    ~Scanner();
    int nextInt(void)
    {
        int variable;
        std::cin>>variable;
        std::cin.sync();
        return variable;
    }
    float nextFloat(void)
    {
        float variable;
        std::cin>>variable;
        std::cin.sync();
        return variable;
    }
    template <typename T>
    T next(void)
    {
        T variable;
        std::cin>>variable;
        std::cin.sync();
        return variable;
    }
    String nextLine(void)
    {
        String variable;
        getline(std::cin,variable);
        std::cin.sync();
        return variable;
    }
};
}
}
#endif 


How can I use Scanner MyScan = new Scanner(System.in)? Wich changes are needed?

Thanks.
Hi,

Since the constructor you build for the class Scanner asks for a parameter of type String, you must provide this type of parameter. Now, if System.in is of type String then it's partly ok. Partly because with the new keyword you actually create a pointer, so the correct line would be:

 
Scanner *MyScan = new Scanner(System.in);


This will work if System.in is of type String.

The problem is : this CANNOT be a pointer. There is an way?
If you are misundestood, see this: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Thanks in advance.
Ok, I see what you want. According to this:
http://www.cprogramming.com/java/c-and-c++-for-java-programmers.html
You then can simply have an instance of your Scanner class like this.

Scanner MyScan(System.in);

This way, you use the constructor that takes a String as parameter, and you create an object (not a pointer) of the class Scanner.

I hope it is what you want.
@aleonard
Yes, thank you, but I know this. There is another way to do EXACTLY as the way that I want? (Scanner object = new Scanner(System.in))
> do EXACTLY as the way that I want? (Scanner object = new Scanner(System.in))

Yes, but you have to write a bit of code if you want it to work just as a reference in Java would.
(A terrible idea unless it is purely for purposes of learning C++. Why would anyone want to mimic in C++ the constructs of a language which has no notion of value semantics, no notion of const-correctness and no notion of RAII?)

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <functional>
#include <memory>
#include <stdexcept>

class scanner_impl
{
        explicit scanner_impl( int i = 0 ) : val(i) {}

        int value() const { return val ; }
        int& value() { return val ; }

        void value( int v ) { val = v ; }

        int foobar() const { return val * 5 ; }

        int val ;

        friend class Scanner ;
        friend class std::shared_ptr<scanner_impl> ;
};

struct NullPointerException : virtual public std::runtime_error
{
    using base = std::runtime_error ;
    using base::base ;
    NullPointerException( const char* cstr ) : base(cstr) {}
};

struct OutOfMemoryError : virtual public std::runtime_error
{
    using base = std::runtime_error ;
    using base::base ;
    OutOfMemoryError( const char* cstr ) : base(cstr) {}
};

constexpr struct {} null ;

struct Scanner
{
    Scanner() = default ;
    explicit Scanner( int i ) : impl( new (std::nothrow) scanner_impl(i) )
    { if( !impl ) throw OutOfMemoryError("out of memory") ; }
    Scanner( Scanner* ptr ) : impl(ptr->impl) {} // crazy!

    Scanner& operator=( const decltype(null)& ) { impl.reset() ; return *this ; }

    int getValue() /*const*/ { assert_not_null() ; return impl->value() ; }

    void setValue( int v ) { assert_not_null() ; impl->value(v) ; }

    int fooBar() /*const*/ { assert_not_null() ; return impl->foobar() ; }

    private:
        std::shared_ptr<scanner_impl> impl ;
        void assert_not_null() const
        { if( !impl ) throw NullPointerException("NullPointerException") ; }

    friend bool operator== ( const decltype(null)&, const Scanner& s )
    { return !s.impl ;}
};

bool operator!= ( const decltype(null)& n, const Scanner& s ) { return !( n == s ) ;}
bool operator== ( const Scanner& s, const decltype(null)& n ) { return n == s ;}
bool operator!= ( const Scanner& s, const decltype(null)& n ) { return n != s ;}

int main()
{
    Scanner theScanner = new Scanner(25) ;

    std::cout << theScanner.getValue() << ' ' << theScanner.fooBar() << '\n' ;
    theScanner.setValue(100) ;
    std::cout << theScanner.getValue() << '\n' ;

    theScanner = new Scanner( -9 ) ;
    std::cout << theScanner.getValue() << ' ' << theScanner.fooBar() << '\n' ;

    theScanner = null ;

    if( theScanner !=  null ) std::cout << theScanner.getValue() << '\n' ;
    else std::cout << "null reference\n" ;

    try { std::cout << theScanner.getValue() << '\n' ; }
    catch( const std::exception& e ) { std::cerr << e.what() << '\n' ; }
}

http://coliru.stacked-crooked.com/a/704ee557853366d9
Thank you! And yes, it's only for learning. Thank you, again!
ERRORS:
1
2
3
4
5
6
C:\Users\Lucas\Desktop\Projetos\C++\teste.cpp|26|error: no members matching 'NullPointerException::base {aka std::runtime_error}::base' in 'using base = class std::runtime_error {aka class std::runtime_error}'|
C:\Users\Lucas\Desktop\Projetos\C++\teste.cpp|33|error: no members matching 'OutOfMemoryError::base {aka std::runtime_error}::base' in 'using base = class std::runtime_error {aka class std::runtime_error}'|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 3 seconds) ===|


:(

You are using an older version of the compiler; one that does not support all C++ language features.
Try this modified code (without type aliases, inherited constructors and defaulted member functions):

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <functional>
#include <memory>
#include <stdexcept>

class scanner_impl
{
        explicit scanner_impl( int i = 0 ) : val(i) {}

        int value() const { return val ; }
        int& value() { return val ; }

        void value( int v ) { val = v ; }

        int foobar() const { return val * 5 ; }

        int val ;

        friend class Scanner ;
        friend class std::shared_ptr<scanner_impl> ;
};

struct NullPointerException : virtual public std::runtime_error
{
    // using base = std::runtime_error ; // ********
    typedef std::runtime_error base ; // ********
    // using base::base ; // ********

    NullPointerException( const char* cstr ) : base(cstr) {}
};

struct OutOfMemoryError : virtual public std::runtime_error
{
    // using base = std::runtime_error ; // ********
    typedef std::runtime_error base ; // ********
    // using base::base ; // ********

    OutOfMemoryError( const char* cstr ) : base(cstr) {}
};

// constexpr struct {} null ; // ********
const struct {} null ; // ********

struct Scanner
{
    //Scanner() = default ; // ********
    Scanner() {} // ********

    explicit Scanner( int i ) : impl( new (std::nothrow) scanner_impl(i) )
    { if( !impl ) throw OutOfMemoryError("out of memory") ; }
    Scanner( Scanner* ptr ) : impl(ptr->impl) {} // crazy!

    Scanner& operator=( const decltype(null)& ) { impl.reset() ; return *this ; }

    int getValue() /*const*/ { assert_not_null() ; return impl->value() ; }

    void setValue( int v ) { assert_not_null() ; impl->value(v) ; }

    int fooBar() /*const*/ { assert_not_null() ; return impl->foobar() ; }

    private:
        std::shared_ptr<scanner_impl> impl ;
        void assert_not_null() const
        { if( !impl ) throw NullPointerException("NullPointerException") ; }

    friend bool operator== ( const decltype(null)&, const Scanner& s )
    { return !s.impl ;}
};

bool operator!= ( const decltype(null)& n, const Scanner& s ) { return !( n == s ) ;}
bool operator== ( const Scanner& s, const decltype(null)& n ) { return n == s ;}
bool operator!= ( const Scanner& s, const decltype(null)& n ) { return n != s ;}

int main()
{
    Scanner theScanner = new Scanner(25) ;

    std::cout << theScanner.getValue() << ' ' << theScanner.fooBar() << '\n' ;
    theScanner.setValue(100) ;
    std::cout << theScanner.getValue() << '\n' ;

    theScanner = new Scanner( -9 ) ;
    std::cout << theScanner.getValue() << ' ' << theScanner.fooBar() << '\n' ;

    theScanner = null ;

    if( theScanner !=  null ) std::cout << theScanner.getValue() << '\n' ;
    else std::cout << "null reference\n" ;

    try { std::cout << theScanner.getValue() << '\n' ; }
    catch( const std::exception& e ) { std::cerr << e.what() << '\n' ; }
}
Topic archived. No new replies allowed.