When to mark `JNIEnv *env` const? - java-native-interface

The question is really simple, as I am still learning about it:
When should one mark JNIEnv *env as const JNIEnv *env?
Are there any implications that a newbie wouldn't think of?
I've googled but couldn't find much related information/discussion.
Thanks!

Related

SQLite char* conversion in C++

I've recently been relearning C++ and creating an application in hand with what I'm learning and even branching out to figure out concepts that aren't necessarily normal. As dangerous as I see this being, I am still diving head first.
That being said the application I'm currently working on requires storing some info in a local database which I will then be merging with a server database at some point in the future to allow for more in-depth queries and a better UI. While diving into learning SQLite3 integration with C++ I've found that a lot of the integration is specifically "C/C++" with, what appears to be, a stronger foot in C than C++. With this realization I've come across one very specific tutorial that leaned on C++ over C minus the specific issue I'm encountering.
https://www.dreamincode.net/forums/topic/122300-sqlite-in-c/
I actually rather like the concise nature of the Database.cpp that the author of the tutorial created and I want to utilize it. The problem is C++ likes to throw the conversion warnings that apparently work due to the use of C, but are deprecated in C++.
ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
Technically this can be bypassed by casting the string to (char*). While I understand this it seems I may be missing some information as well. Could this be bypassed by changing the parameters of the Database function to "string*" then converting it to "char*" in the function or should I not care about the implicit conversion and just ignore it. I really don't want to cross C and C++ in my application so I would prefer to paid heed to the warning. Figured I'd ask for advice to at least get some clarification though.
If it seems obvious from my inquiry that I am lacking in some very specific section of my C++ knowledge please feel free to let me know. I am nothing if not diligent when it comes to making sure I can fill all the gaps in my knowledge on any given topic.
You should tell the author of that tutorial to make his code const correct if you want to make use of his class.
Well, actually, I suggest you edit it yourself to make it conform. This won't take you long once you understand the principles involved (which are not hard to get your head around), and doing this will help you write your own code in the right way.
So, just by way of example, change this:
class Database
{
public:
Database(char* filename);
...
To this (note the added const):
class Database
{
public:
Database(const char* filename);
...
There are, of course, a bunch of other related changes you need to make but you just have to see it through, and the compiler itself will guide you when you get it wrong. SQLlite itself is already const correct (because those guys are professionals), so there is light at the end of the tunnel.

C++: Use auto, or typedef? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I'd hate to ask an intentionally subjective question on here, but I don't have anyone else to ask this directly...
I'm trying to "lead by example" in my team by adopting more modern C++ concepts in new code I write. My coworkers are a bit shy of templates, and gripe about having to type unique_ptr<Foobar> or shared_ptr<Foobar> instead of just Foobar* when utilizing some classes I've recently created (factory methods return unique_ptrs and I store lists of these objects in vectors using shared_ptr).
They twisted my arm and convinced me to typedef these into something easier to type, eg FoobarUniquePtr.
But now I'm in a mess of const-correctness with these typedefs. I'd have to define extra typedefs for each const and non const type, and const const_FoobarUniquePtr doesn't seem to properly express the const semantics.
So, to my question, would it make sense to stop using these typedefs and instead shove auto at my teammates when they complain about having to type the templated smart pointers? I'm also open to other alternatives.
In general, auto makes your life a lot easier.
If you aren't shipping on any esoteric architectures (and have no plans to do so anytime soon), the answer should be "use auto whenever you can".
I guess you need to define all variations you might need.
Get your guys to see the presentaion from Bjarne Stoustrup and read some of the blogs in C++11. As for templates buy some of the books on the book list and "coincedentially" drop some of them on your desk to start the discussion.
You can even write a few powerpoint presentions, and present them to your team members and build up a culture of research.
Bjarne Stroustrup: C++11 Style
book list: https://stackoverflow.com/tags/c%2b%2b/info
I'd have to define extra typedefs for each const and non const type
... which will cause a combinatorial blowup as the number of places where a const fits in the type increases. Explain to your colleagues that the amount of work you're doing increases exponentially because they're fighting the language instead of using it. And yes, use auto when appropriate.
When I create a new class one of the first things I do is:
class MyClass
{
public: // Typedefs
typedef std::shared_ptr<MyClass> Ptr;
typedef std::shared_ptr<const MyClass> ConstPtr;
};
Then user's of my class can use it like:
MyClass::Ptr pMyClass = std::make_shared<MyClass>();
The reason I add them to the class as public typedefs is I hate to pollute the global namespace.

How can I study and practices on C++ pointer?

I'm moving from C# to C++, one of priority topic for me is pointer. I'm reading some book and some blog post about pointer and I understand its basic concept. Now I want to learn about pointer by practicing it.
I try to search on google, unfortunately not thing found. Are there anything can help me study and practices on C++ pointer?
Just google for "learn C pointers". For example, you'll find stuff like http://computer.howstuffworks.com/c20.htm
Once you understand what a pointer is, what it does, and how it is managed, start using C++ smart pointers which take care of some of the grunt work for you.
I would agree with GMan's comment that a good C++ book is the way to go to do this properly.
Aside from that the most important thing when learning pointers in C++ is to use smart pointers. These can be found in the appropriate boost library. Using these from the outset will save you much pain in the future.
Also boost is a GREAT resource for any C++ programmer and you should familiarise yourself with it.
Hope these links work for you. I found it easily on google.
http://www.sethi.org/classes/comp217/lab_notes/lab_10_pointers.html#ASSIGNMENT
http://condor.depaul.edu/~ntomuro/courses/309/notes/pointer_exercises.html

Transiting from COBOL to C++

I am a fairly junior programmer and have the task of interviewing an experienced mainframe COBOL programmer for a position doing C++ development for Windows Mobile. By experienced I mean that the candidate's entire programming career has been COBOL.
I am not biased against any particular language, just slightly concerned about my ability to do a thorough interview.
Inside of an interview how can I, with no COBOL experience, get a good feel for the abilities of this interviewee?
Are there any specific things I should ask or look for?
I have already read up on a lot of general interview stuff on SO, what would be helpful are any specific things I look for given the nature of the candidate's background.
The best thing is, give him a task, similar to what he will have to do at your company.
Tell him he can use pseudo code (So no Internet is needed).
Also, The main problem Cobol people have is to grasp OO (Since Cobol is mostly procedural...I am aware of new OO versions).
One more pitfall Cobol people have is grasping the scope concept, As traditional Cobol each variable live from start to bottom of the app he is in.
If this person is a good programmer at all, he/she have had experience with other languages. (Speaking as an old fart who started with COBOL and even worse RPG/II.) Even if this person have never had a job with anything else.
Here are some things you might ask:
"How did you get started with computers?"
"Do you do any programming for fun?"
"Why do you want to move to a C++ environment?" (This is an especially good question because good COBOL programmers are actually hard to find now. I'll bet this person wants to learn something new.)
"Have you done any assembly language programming?" (Odds are much better they'll take to C/C++ quickly if they are comfortable with the machine.)
"Can you describe what 'object oriented programming' means?" (Keys: look for a description of what "objects" are, "inheritance", and "polymorphism". If that works out, ask when they'd use inheritance vs aggregation, ie, "is a" relations vs "has a" relations.)
"Do you have a Windows computer at home or do you have a real computer?"
If they claim any C++ knowledge, a really good set of screening questions are:
"What is the difference between a declaration and a definition in C?"
"What are the differences among 'private', 'protected' and 'public' declarations in a class?"
"What is a 'friend'?" (Extra credit if they know to say that "In C++ only your friends can access your private parts.")
"What is a virtual member function?"
"What is 'virtual inheritance'?"
Make sure they know and understand structs/classes, types, polymorphism, templates, functions, and most importantly pointers.
Make sure they understand the syntax. How would they declare a pointer to a void function that takes two integer arguments, for example? How would they solve the most vexing parse?
How can initialization lists optimize code? Are they familiar with the Standard Template Library?
Can they hand-code a primitive linked list or vector array by hand?
These things are what you want in a programmer capable of writing efficient and clean code.
Isn't it more important to figure out how good the interviewee is in regards to C++, if that's what he is supposed to work on?

Sample C++ tests

I'm about to take a C++ test. But I only get one crack at it to get over 85%. If I don't push over that, then I don't get the job.
The problem with these tests are that they typically target generic C++, and depending on what libraries you use your definition of generic may differ. STL and Boost may seem logical to some (and should be part of most) but I worked with MFC for years before ever using templates. Why use >vector> when you've got access to CArray? (retorical question)
If you've worked with dialogs you've not used stdio. If you've worked with Borland products you've not used MFC. If you've worked with Palm, you've not used the file system, and you've definitely not used CFile.
OK, so here's the question...
Given that I'd like to pass the 85%, I'm taking online tests of "generic" C++. So... Is there a place I could go to find tests? The more the better. Correct answers are also good, either during or after the test. As long as I can learn from my mistakes.
EDIT: If your answer doesn't have a link to a test, some C++ questions, or some interview questions... You missed the point of Is there a place I could go to find tests?
Great example.. I've just found this question.
What does the following code fragment print? cout << setw(6) << setfill('#') << "Hello";
I've been coding for 9 years. And never used cout, setw or setfill once. Not since university.
Erase all the MFC from your head for now. Go pick up a book like The C++ Programming Language, and try to learn the concepts front to back. You should be fine. If they are asking for more than this, I don't want to know what their definition of "generic" is.
The few times I've been "tested" (well "interviewed"), folks were far more concerned with questions like:
What is Object Oriented Programming? OOA (analysis)? OOD (design)? UML?
When should you inherit from a class? When should a class be aggregated?
What are virtual methods? What are pure virtual methods? What is the vtable?
Sibling cast problem. class C : public A, public B; C c; B * b = & c; How to cast object b (type B*) to an A*?
What does the stack look like as a simple program executes?
Differences between heap/stack?
How does new() differ from malloc()?
etc.
There's lots of previous discussion on C++ interviewing questions here on StackOverflow and elsewhere:
https://stackoverflow.com/questions/240212/
what-is-the-difference-between-newdelete-and-mallocfree
https://stackoverflow.com/questions/347793/
c-areas-you-look-for-during-interview
https://stackoverflow.com/questions/365823/
what-kinds-of-interview-questions-are-appropriate-for-a-c-phone-screen
http://www.joelonsoftware.com/articles/GuerrillaInterviewing3.html
Just to add my two cents here: If they are looking for graphic details... To see if you've memorized the entire C++ spec... Well I know the economy stinks right now but it is improving, there are other jobs out there, and you NEED to find one of them. Interviews are a two-way street. If they are into nit-picking details, this is NOT a place you want to work.
You might try Herb Sutter's book Exceptional C++; it contains items organized like questions and is, in my opinion, very clear and very well written. I don't know if it will be directly useful for the interview, but it makes you think about aspects of the language you had never considered before.
It's been a long time since I last visited it, but you might also try this site with interview questions: geekinterview.com - take a look in particular at the C++ section.
All the best for your interview :)
What does the following code fragment print? cout << setw(6) << setfill('#') << "Hello";
It prints the following sentence to standard output:
Please do not work for us. We have no clue about what it means to be a
good software developer.
I found these.
http://www.acuit.com/_vti_bin/shtml.dll/Test_C++.htm
http://www.acetheinterview.com/questions/cats/index.php/algorithm
http://www.faqs.org/faqs/C-faq/faq/
http://www.coolinterview.com/type.asp?iType=41
http://www.radiussg.com.au/Candidate%20Interview%20Guide.pdf
http://www.eecs.utoledo.edu/~ledgard/oop/left.html
Some questions in FAQ's might work as tests.
IF you are going to give tests for job then brainbench tests may help.
I guess C++ tests are free and you can get some idea of what kind of questions you can get.
Good luck for tests!
During interviews I bother about the candidate being able to show me that he/she understands what he's/she's doing and that he's/she's leaning toward "modern" C++ (i.e. template intensive).
He/she also needs to understand some subtilities of the languages, but not the most arcane. I don't ask tricky question that are based on the oddities of the language. Why?
STL mastery is a pre-requisite. I see knowing nothing about Boost as a bad sign.
If I were to write a test, I would make it quite easy just to filter the really bad programmers that don't master the syntax and the logic of C++. I however prefer a one hour one-to-one interview to filter candidates.
If you find yourself fighting against a very hard written C++ test : run away.
I hope this helps.
Edit : if you really want tests and questions, check this out : http://www.gotw.ca/gotw/
There are a few free tests here and they have explanation videos on youtube for a few of the questions.